From d587548250b169b1ffc5f36cad0169004df48ea7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 8 Apr 2023 14:53:55 -0400 Subject: [PATCH] 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 6da0a72..4d6b663 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 6176e53..72e73f8 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) {