diff --git a/example/ssesvr.cc b/example/ssesvr.cc index d18aed3..5b0e0b9 100644 --- a/example/ssesvr.cc +++ b/example/ssesvr.cc @@ -19,7 +19,7 @@ public: unique_lock 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) { diff --git a/httplib.h b/httplib.h index 4dcfbac..8ac01bc 100644 --- a/httplib.h +++ b/httplib.h @@ -340,7 +340,6 @@ public: std::function write; std::function done; - std::function is_writable; std::ostream os; private: @@ -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; @@ -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; } @@ -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; } @@ -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; @@ -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; } @@ -6544,8 +6549,6 @@ inline std::unique_ptr 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; @@ -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; diff --git a/test/test.cc b/test/test.cc index 2c5ad4f..10ff9f8 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1650,7 +1650,6 @@ protected: [&](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"; @@ -1664,7 +1663,6 @@ protected: 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; @@ -1694,7 +1692,6 @@ protected: 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 = @@ -1714,8 +1711,6 @@ protected: 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; }); @@ -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; }, @@ -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; @@ -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; }, @@ -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;