Skip to content

Commit df20c27

Browse files
authored
resolve http server can't send file large than 2GB (Fix yhirose#1290) (yhirose#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.
1 parent a5a6276 commit df20c27

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

httplib.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4691,7 +4691,7 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) {
46914691
inline ssize_t SocketStream::write(const char *ptr, size_t size) {
46924692
if (!is_writable()) { return -1; }
46934693

4694-
#ifdef _WIN32
4694+
#if defined(_WIN32) && !defined(_WIN64)
46954695
size =
46964696
(std::min)(size, static_cast<size_t>((std::numeric_limits<int>::max)()));
46974697
#endif

test/test.cc

+34
Original file line numberDiff line numberDiff line change
@@ -4742,6 +4742,40 @@ TEST(SendAPI, SimpleInterface_Online) {
47424742
EXPECT_EQ(301, res->status);
47434743
}
47444744

4745+
// Disabled due to out-of-memory problem on GitHub Actions
4746+
#ifdef _WIN64
4747+
TEST(ServerLargeContentTest, DISABLED_SendLargeContent) {
4748+
// allocate content size larger than 2GB in memory
4749+
const size_t content_size = 2LL * 1024LL * 1024LL * 1024LL + 1LL;
4750+
char *content = (char *)malloc(content_size);
4751+
ASSERT_TRUE(content);
4752+
4753+
Server svr;
4754+
svr.Get("/foo", [=](const httplib::Request &req, httplib::Response &resp) {
4755+
resp.set_content(content, content_size, "application/octet-stream");
4756+
});
4757+
4758+
auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); });
4759+
while (!svr.is_running()) {
4760+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
4761+
}
4762+
4763+
// Give GET time to get a few messages.
4764+
std::this_thread::sleep_for(std::chrono::seconds(1));
4765+
4766+
Client cli(HOST, PORT);
4767+
auto res = cli.Get("/foo");
4768+
ASSERT_TRUE(res);
4769+
EXPECT_EQ(200, res->status);
4770+
EXPECT_EQ(content_size, res->body.length());
4771+
4772+
free(content);
4773+
svr.stop();
4774+
listen_thread.join();
4775+
ASSERT_FALSE(svr.is_running());
4776+
}
4777+
#endif
4778+
47454779
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
47464780
TEST(YahooRedirectTest2, SimpleInterface_Online) {
47474781
Client cli("http://yahoo.com");

test/test.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
<AdditionalUsingDirectories>
117117
</AdditionalUsingDirectories>
118118
<SDLCheck>true</SDLCheck>
119+
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
119120
</ClCompile>
120121
<Link>
121122
<SubSystem>Console</SubSystem>
@@ -158,6 +159,7 @@
158159
<AdditionalUsingDirectories>
159160
</AdditionalUsingDirectories>
160161
<SDLCheck>true</SDLCheck>
162+
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
161163
</ClCompile>
162164
<Link>
163165
<SubSystem>Console</SubSystem>

0 commit comments

Comments
 (0)