Skip to content

Removed is_writable() from DataSink #1483

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

Merged
merged 1 commit into from
Feb 4, 2023
Merged
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
2 changes: 1 addition & 1 deletion example/ssesvr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EventDispatcher {
unique_lock<mutex> lk(m_);
int id = id_;
cv_.wait(lk, [&] { return cid_ == id; });
if (sink->is_writable()) { sink->write(message_.data(), message_.size()); }
sink->write(message_.data(), message_.size());
}

void send_event(const string &message) {
Expand Down
44 changes: 23 additions & 21 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ class DataSink {

std::function<bool(const char *data, size_t data_len)> write;
std::function<void()> done;
std::function<bool()> is_writable;
std::ostream os;

private:
Expand Down Expand Up @@ -3632,7 +3631,7 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider,

data_sink.write = [&](const char *d, size_t l) -> bool {
if (ok) {
if (write_data(strm, d, l)) {
if (strm.is_writable() && write_data(strm, d, l)) {
offset += l;
} else {
ok = false;
Expand All @@ -3641,14 +3640,14 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider,
return ok;
};

data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };

while (offset < end_offset && !is_shutting_down()) {
if (!content_provider(offset, end_offset - offset, data_sink)) {
if (!strm.is_writable()) {
error = Error::Write;
return false;
} else if (!content_provider(offset, end_offset - offset, data_sink)) {
error = Error::Canceled;
return false;
}
if (!ok) {
} else if (!ok) {
error = Error::Write;
return false;
}
Expand Down Expand Up @@ -3680,18 +3679,21 @@ write_content_without_length(Stream &strm,
data_sink.write = [&](const char *d, size_t l) -> bool {
if (ok) {
offset += l;
if (!write_data(strm, d, l)) { ok = false; }
if (!strm.is_writable() || !write_data(strm, d, l)) { ok = false; }
}
return ok;
};

data_sink.done = [&](void) { data_available = false; };

data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };

while (data_available && !is_shutting_down()) {
if (!content_provider(offset, 0, data_sink)) { return false; }
if (!ok) { return false; }
if (!strm.is_writable()) {
return false;
} else if (!content_provider(offset, 0, data_sink)) {
return false;
} else if (!ok) {
return false;
}
}
return true;
}
Expand Down Expand Up @@ -3720,7 +3722,10 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider,
// Emit chunked response header and footer for each chunk
auto chunk =
from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n";
if (!write_data(strm, chunk.data(), chunk.size())) { ok = false; }
if (!strm.is_writable() ||
!write_data(strm, chunk.data(), chunk.size())) {
ok = false;
}
}
} else {
ok = false;
Expand Down Expand Up @@ -3759,14 +3764,14 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider,
}
};

data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };

while (data_available && !is_shutting_down()) {
if (!content_provider(offset, 0, data_sink)) {
if (!strm.is_writable()) {
error = Error::Write;
return false;
} else if (!content_provider(offset, 0, data_sink)) {
error = Error::Canceled;
return false;
}
if (!ok) {
} else if (!ok) {
error = Error::Write;
return false;
}
Expand Down Expand Up @@ -6544,8 +6549,6 @@ inline std::unique_ptr<Response> ClientImpl::send_with_content_provider(
return ok;
};

data_sink.is_writable = [&](void) { return ok && true; };

while (ok && offset < content_length) {
if (!content_provider(offset, content_length - offset, data_sink)) {
error = Error::Canceled;
Expand Down Expand Up @@ -6717,7 +6720,6 @@ inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider(
bool has_data = true;
cur_sink.write = sink.write;
cur_sink.done = [&]() { has_data = false; };
cur_sink.is_writable = sink.is_writable;

if (!provider_items[cur_item].provider(offset - cur_start, cur_sink))
return false;
Expand Down
9 changes: 0 additions & 9 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,6 @@ class ServerTest : public ::testing::Test {
[&](const Request & /*req*/, Response &res) {
res.set_chunked_content_provider(
"text/plain", [](size_t /*offset*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "123";
sink.os << "456";
sink.os << "789";
Expand All @@ -1664,7 +1663,6 @@ class ServerTest : public ::testing::Test {
res.set_chunked_content_provider(
"text/plain",
[i](size_t /*offset*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
switch (*i) {
case 0: sink.os << "123"; break;
case 1: sink.os << "456"; break;
Expand Down Expand Up @@ -1694,7 +1692,6 @@ class ServerTest : public ::testing::Test {
res.set_content_provider(
data->size(), "text/plain",
[data](size_t offset, size_t length, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
size_t DATA_CHUNK_SIZE = 4;
const auto &d = *data;
auto out_len =
Expand All @@ -1714,8 +1711,6 @@ class ServerTest : public ::testing::Test {
res.set_content_provider(
size_t(-1), "text/plain",
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
if (!sink.is_writable()) return false;

sink.os << "data_chunk";
return true;
});
Expand Down Expand Up @@ -2952,7 +2947,6 @@ TEST_F(ServerTest, PutWithContentProvider) {
auto res = cli_.Put(
"/put", 3,
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
return true;
},
Expand All @@ -2979,7 +2973,6 @@ TEST_F(ServerTest, PutWithContentProviderWithoutLength) {
auto res = cli_.Put(
"/put",
[](size_t /*offset*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
sink.done();
return true;
Expand All @@ -3006,7 +2999,6 @@ TEST_F(ServerTest, PutWithContentProviderWithGzip) {
auto res = cli_.Put(
"/put", 3,
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
return true;
},
Expand Down Expand Up @@ -3035,7 +3027,6 @@ TEST_F(ServerTest, PutWithContentProviderWithoutLengthWithGzip) {
auto res = cli_.Put(
"/put",
[](size_t /*offset*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
sink.done();
return true;
Expand Down