From df20c27696c6dcb2b9ccecf3c4f9c3d06c1ecf8c Mon Sep 17 00:00:00 2001 From: conghuawang Date: Fri, 27 May 2022 23:56:20 +0800 Subject: [PATCH] resolve http server can't send file large than 2GB (Fix #1290) (#1294) * resolve problem: http server can't send file large than 2GB. add unit test for http server send large file. add /bigobj compile option to msvc x64. * disable unit test "ServerLargeContentTest" due to out-of-memory on GitHub Actions. --- httplib.h | 2 +- test/test.cc | 34 ++++++++++++++++++++++++++++++++++ test/test.vcxproj | 2 ++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 13a443a..fae94d9 100644 --- a/httplib.h +++ b/httplib.h @@ -4691,7 +4691,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; } -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_WIN64) size = (std::min)(size, static_cast((std::numeric_limits::max)())); #endif diff --git a/test/test.cc b/test/test.cc index adec943..322b208 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4742,6 +4742,40 @@ TEST(SendAPI, SimpleInterface_Online) { EXPECT_EQ(301, res->status); } +// Disabled due to out-of-memory problem on GitHub Actions +#ifdef _WIN64 +TEST(ServerLargeContentTest, DISABLED_SendLargeContent) { + // allocate content size larger than 2GB in memory + const size_t content_size = 2LL * 1024LL * 1024LL * 1024LL + 1LL; + char *content = (char *)malloc(content_size); + ASSERT_TRUE(content); + + Server svr; + svr.Get("/foo", [=](const httplib::Request &req, httplib::Response &resp) { + resp.set_content(content, content_size, "application/octet-stream"); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); }); + while (!svr.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)); + + Client cli(HOST, PORT); + auto res = cli.Get("/foo"); + 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 + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT TEST(YahooRedirectTest2, SimpleInterface_Online) { Client cli("http://yahoo.com"); diff --git a/test/test.vcxproj b/test/test.vcxproj index 22739d6..b169311 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -116,6 +116,7 @@ true + /bigobj %(AdditionalOptions) Console @@ -158,6 +159,7 @@ true + /bigobj %(AdditionalOptions) Console