From 5a43bb8149e001625f1a0d81e5300e4e997c5ee8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 2 Jun 2021 13:28:49 -0400 Subject: [PATCH] Implemented #946 in a different way --- README.md | 2 +- httplib.h | 12 ++++++++---- test/test.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 45a0e12..8410be5 100644 --- a/README.md +++ b/README.md @@ -266,7 +266,7 @@ svr.Get("/stream", [&](const Request &req, Response &res) { sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE)); return true; // return 'false' if you want to cancel the process. }, - [data] { delete data; }); + [data](bool success) { delete data; }); }); ``` diff --git a/httplib.h b/httplib.h index d96a7b8..d3cddc9 100644 --- a/httplib.h +++ b/httplib.h @@ -337,7 +337,7 @@ using ContentProvider = using ContentProviderWithoutLength = std::function; -using ContentProviderResourceReleaser = std::function; +using ContentProviderResourceReleaser = std::function; using ContentReceiverWithProgress = std::function(1024)); + std::string out(out_len, '@'); + sink.write(out.data(), out_len); + return offset < 4096; + }, + [](bool success) { ASSERT_FALSE(success); }); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", 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("localhost", PORT); + + auto res = cli.Get("/hi", [&](const char * /*data*/, size_t /*data_length*/) { + return false; + }); + + ASSERT_FALSE(res); + + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); +} + TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) { Server svr;