Skip to content

content provider: add result callback to content provider #946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,13 @@ struct Response {

void set_content_provider(
size_t length, const char *content_type, ContentProvider provider,
const std::function<void()> &resource_releaser = nullptr);
const std::function<void()> &resource_releaser = nullptr,
const std::function<void(bool)>& result_callback = nullptr);

void set_content_provider(
const char *content_type, ContentProviderWithoutLength provider,
const std::function<void()> &resource_releaser = nullptr);
const std::function<void()> &resource_releaser = nullptr,
const std::function<void(bool)> &result_callback = nullptr);

void set_chunked_content_provider(
const char *content_type, ContentProviderWithoutLength provider,
Expand All @@ -471,6 +473,7 @@ struct Response {
size_t content_length_ = 0;
ContentProvider content_provider_;
std::function<void()> content_provider_resource_releaser_;
std::function<void(bool)> content_provider_result_callback_;
bool is_chunked_content_provider_ = false;
};

Expand Down Expand Up @@ -4033,23 +4036,27 @@ inline void Response::set_content(const std::string &s,
inline void
Response::set_content_provider(size_t in_length, const char *content_type,
ContentProvider provider,
const std::function<void()> &resource_releaser) {
const std::function<void()> &resource_releaser,
const std::function<void(bool)>& result_callback) {
assert(in_length > 0);
set_header("Content-Type", content_type);
content_length_ = in_length;
content_provider_ = std::move(provider);
content_provider_resource_releaser_ = resource_releaser;
content_provider_result_callback_ = result_callback;
is_chunked_content_provider_ = false;
}

inline void
Response::set_content_provider(const char *content_type,
ContentProviderWithoutLength provider,
const std::function<void()> &resource_releaser) {
const std::function<void()> &resource_releaser,
const std::function<void(bool)> &result_callback) {
set_header("Content-Type", content_type);
content_length_ = 0;
content_provider_ = detail::ContentProviderAdapter(std::move(provider));
content_provider_resource_releaser_ = resource_releaser;
content_provider_result_callback_ = result_callback;
is_chunked_content_provider_ = false;
}

Expand Down Expand Up @@ -5232,7 +5239,11 @@ Server::process_request(Stream &strm, bool close_connection,

if (routed) {
if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; }
return write_response_with_content(strm, close_connection, req, res);
bool success = write_response_with_content(strm, close_connection, req, res);
if (res.content_provider_result_callback_) {
res.content_provider_result_callback_(success);
}
return success;
} else {
if (res.status == -1) { res.status = 404; }
return write_response(strm, close_connection, req, res);
Expand Down
34 changes: 34 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,27 @@ class ServerTest : public ::testing::Test {
return true;
});
})
.Get("/streamed-result-success",
[&](const Request & /*req*/, Response &res) {
res.set_content_provider(
6, "text/plain",
[](size_t offset, size_t /*length*/, DataSink &sink) {
sink.os << (offset < 3 ? "a" : "b");
return true;
},
[]() {},
[](bool success) { EXPECT_TRUE(success); }
);
})
.Get("/streamed-result-failure",
[&](const Request & /*req*/, Response &res) {
res.set_content_provider(
6, "text/plain",
[](size_t offset, size_t /*length*/, DataSink &sink) {
return false;
},
[]() {}, [](bool success) { EXPECT_FALSE(success); });
})
.Get("/streamed-with-range",
[&](const Request & /*req*/, Response &res) {
auto data = new std::string("abcdefg");
Expand Down Expand Up @@ -2324,6 +2345,19 @@ TEST_F(ServerTest, GetStreamed) {
EXPECT_EQ(std::string("aaabbb"), res->body);
}

TEST_F(ServerTest, GetStreamedResultSuccess) {
auto res = cli_.Get("/streamed-result-success");
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
EXPECT_EQ("6", res->get_header_value("Content-Length"));
EXPECT_EQ(std::string("aaabbb"), res->body);
}

TEST_F(ServerTest, GetStreamedResultFailure) {
auto res = cli_.Get("/streamed-result-failure");
ASSERT_FALSE(res);
}

TEST_F(ServerTest, GetStreamedWithRange1) {
auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}});
ASSERT_TRUE(res);
Expand Down