Skip to content

Commit d663588

Browse files
authored
Removed is_writable() from DataSink (Resolve #1478, too) (#1483)
1 parent 439caf5 commit d663588

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

example/ssesvr.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class EventDispatcher {
1919
unique_lock<mutex> lk(m_);
2020
int id = id_;
2121
cv_.wait(lk, [&] { return cid_ == id; });
22-
if (sink->is_writable()) { sink->write(message_.data(), message_.size()); }
22+
sink->write(message_.data(), message_.size());
2323
}
2424

2525
void send_event(const string &message) {

httplib.h

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ class DataSink {
340340

341341
std::function<bool(const char *data, size_t data_len)> write;
342342
std::function<void()> done;
343-
std::function<bool()> is_writable;
344343
std::ostream os;
345344

346345
private:
@@ -3632,7 +3631,7 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider,
36323631

36333632
data_sink.write = [&](const char *d, size_t l) -> bool {
36343633
if (ok) {
3635-
if (write_data(strm, d, l)) {
3634+
if (strm.is_writable() && write_data(strm, d, l)) {
36363635
offset += l;
36373636
} else {
36383637
ok = false;
@@ -3641,14 +3640,14 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider,
36413640
return ok;
36423641
};
36433642

3644-
data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };
3645-
36463643
while (offset < end_offset && !is_shutting_down()) {
3647-
if (!content_provider(offset, end_offset - offset, data_sink)) {
3644+
if (!strm.is_writable()) {
3645+
error = Error::Write;
3646+
return false;
3647+
} else if (!content_provider(offset, end_offset - offset, data_sink)) {
36483648
error = Error::Canceled;
36493649
return false;
3650-
}
3651-
if (!ok) {
3650+
} else if (!ok) {
36523651
error = Error::Write;
36533652
return false;
36543653
}
@@ -3680,18 +3679,21 @@ write_content_without_length(Stream &strm,
36803679
data_sink.write = [&](const char *d, size_t l) -> bool {
36813680
if (ok) {
36823681
offset += l;
3683-
if (!write_data(strm, d, l)) { ok = false; }
3682+
if (!strm.is_writable() || !write_data(strm, d, l)) { ok = false; }
36843683
}
36853684
return ok;
36863685
};
36873686

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

3690-
data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };
3691-
36923689
while (data_available && !is_shutting_down()) {
3693-
if (!content_provider(offset, 0, data_sink)) { return false; }
3694-
if (!ok) { return false; }
3690+
if (!strm.is_writable()) {
3691+
return false;
3692+
} else if (!content_provider(offset, 0, data_sink)) {
3693+
return false;
3694+
} else if (!ok) {
3695+
return false;
3696+
}
36953697
}
36963698
return true;
36973699
}
@@ -3720,7 +3722,10 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider,
37203722
// Emit chunked response header and footer for each chunk
37213723
auto chunk =
37223724
from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n";
3723-
if (!write_data(strm, chunk.data(), chunk.size())) { ok = false; }
3725+
if (!strm.is_writable() ||
3726+
!write_data(strm, chunk.data(), chunk.size())) {
3727+
ok = false;
3728+
}
37243729
}
37253730
} else {
37263731
ok = false;
@@ -3759,14 +3764,14 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider,
37593764
}
37603765
};
37613766

3762-
data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };
3763-
37643767
while (data_available && !is_shutting_down()) {
3765-
if (!content_provider(offset, 0, data_sink)) {
3768+
if (!strm.is_writable()) {
3769+
error = Error::Write;
3770+
return false;
3771+
} else if (!content_provider(offset, 0, data_sink)) {
37663772
error = Error::Canceled;
37673773
return false;
3768-
}
3769-
if (!ok) {
3774+
} else if (!ok) {
37703775
error = Error::Write;
37713776
return false;
37723777
}
@@ -6544,8 +6549,6 @@ inline std::unique_ptr<Response> ClientImpl::send_with_content_provider(
65446549
return ok;
65456550
};
65466551

6547-
data_sink.is_writable = [&](void) { return ok && true; };
6548-
65496552
while (ok && offset < content_length) {
65506553
if (!content_provider(offset, content_length - offset, data_sink)) {
65516554
error = Error::Canceled;
@@ -6717,7 +6720,6 @@ inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider(
67176720
bool has_data = true;
67186721
cur_sink.write = sink.write;
67196722
cur_sink.done = [&]() { has_data = false; };
6720-
cur_sink.is_writable = sink.is_writable;
67216723

67226724
if (!provider_items[cur_item].provider(offset - cur_start, cur_sink))
67236725
return false;

test/test.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,6 @@ class ServerTest : public ::testing::Test {
16501650
[&](const Request & /*req*/, Response &res) {
16511651
res.set_chunked_content_provider(
16521652
"text/plain", [](size_t /*offset*/, DataSink &sink) {
1653-
EXPECT_TRUE(sink.is_writable());
16541653
sink.os << "123";
16551654
sink.os << "456";
16561655
sink.os << "789";
@@ -1664,7 +1663,6 @@ class ServerTest : public ::testing::Test {
16641663
res.set_chunked_content_provider(
16651664
"text/plain",
16661665
[i](size_t /*offset*/, DataSink &sink) {
1667-
EXPECT_TRUE(sink.is_writable());
16681666
switch (*i) {
16691667
case 0: sink.os << "123"; break;
16701668
case 1: sink.os << "456"; break;
@@ -1694,7 +1692,6 @@ class ServerTest : public ::testing::Test {
16941692
res.set_content_provider(
16951693
data->size(), "text/plain",
16961694
[data](size_t offset, size_t length, DataSink &sink) {
1697-
EXPECT_TRUE(sink.is_writable());
16981695
size_t DATA_CHUNK_SIZE = 4;
16991696
const auto &d = *data;
17001697
auto out_len =
@@ -1714,8 +1711,6 @@ class ServerTest : public ::testing::Test {
17141711
res.set_content_provider(
17151712
size_t(-1), "text/plain",
17161713
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
1717-
if (!sink.is_writable()) return false;
1718-
17191714
sink.os << "data_chunk";
17201715
return true;
17211716
});
@@ -2952,7 +2947,6 @@ TEST_F(ServerTest, PutWithContentProvider) {
29522947
auto res = cli_.Put(
29532948
"/put", 3,
29542949
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
2955-
EXPECT_TRUE(sink.is_writable());
29562950
sink.os << "PUT";
29572951
return true;
29582952
},
@@ -2979,7 +2973,6 @@ TEST_F(ServerTest, PutWithContentProviderWithoutLength) {
29792973
auto res = cli_.Put(
29802974
"/put",
29812975
[](size_t /*offset*/, DataSink &sink) {
2982-
EXPECT_TRUE(sink.is_writable());
29832976
sink.os << "PUT";
29842977
sink.done();
29852978
return true;
@@ -3006,7 +2999,6 @@ TEST_F(ServerTest, PutWithContentProviderWithGzip) {
30062999
auto res = cli_.Put(
30073000
"/put", 3,
30083001
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
3009-
EXPECT_TRUE(sink.is_writable());
30103002
sink.os << "PUT";
30113003
return true;
30123004
},
@@ -3035,7 +3027,6 @@ TEST_F(ServerTest, PutWithContentProviderWithoutLengthWithGzip) {
30353027
auto res = cli_.Put(
30363028
"/put",
30373029
[](size_t /*offset*/, DataSink &sink) {
3038-
EXPECT_TRUE(sink.is_writable());
30393030
sink.os << "PUT";
30403031
sink.done();
30413032
return true;

0 commit comments

Comments
 (0)