Skip to content

Commit 73e0729

Browse files
committed
Change sink.write() to return boolean
1 parent 21c5292 commit 73e0729

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

httplib.h

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class DataSink {
308308
DataSink(DataSink &&) = delete;
309309
DataSink &operator=(DataSink &&) = delete;
310310

311-
std::function<void(const char *data, size_t data_len)> write;
311+
std::function<bool(const char *data, size_t data_len)> write;
312312
std::function<void()> done;
313313
std::function<bool()> is_writable;
314314
std::ostream os;
@@ -2261,8 +2261,7 @@ inline socket_t create_client_socket(
22612261
{
22622262
timeval tv;
22632263
tv.tv_sec = static_cast<long>(write_timeout_sec);
2264-
tv.tv_usec =
2265-
static_cast<decltype(tv.tv_usec)>(write_timeout_usec);
2264+
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(write_timeout_usec);
22662265
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv));
22672266
}
22682267

@@ -3022,14 +3021,15 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider,
30223021
auto ok = true;
30233022
DataSink data_sink;
30243023

3025-
data_sink.write = [&](const char *d, size_t l) {
3024+
data_sink.write = [&](const char *d, size_t l) -> bool {
30263025
if (ok) {
30273026
if (write_data(strm, d, l)) {
30283027
offset += l;
30293028
} else {
30303029
ok = false;
30313030
}
30323031
}
3032+
return ok;
30333033
};
30343034

30353035
data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };
@@ -3068,11 +3068,12 @@ write_content_without_length(Stream &strm,
30683068
auto ok = true;
30693069
DataSink data_sink;
30703070

3071-
data_sink.write = [&](const char *d, size_t l) {
3071+
data_sink.write = [&](const char *d, size_t l) -> bool {
30723072
if (ok) {
30733073
offset += l;
30743074
if (!write_data(strm, d, l)) { ok = false; }
30753075
}
3076+
return ok;
30763077
};
30773078

30783079
data_sink.done = [&](void) { data_available = false; };
@@ -3095,30 +3096,30 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider,
30953096
auto ok = true;
30963097
DataSink data_sink;
30973098

3098-
data_sink.write = [&](const char *d, size_t l) {
3099-
if (!ok) { return; }
3100-
3101-
data_available = l > 0;
3102-
offset += l;
3103-
3104-
std::string payload;
3105-
if (!compressor.compress(d, l, false,
3106-
[&](const char *data, size_t data_len) {
3107-
payload.append(data, data_len);
3108-
return true;
3109-
})) {
3110-
ok = false;
3111-
return;
3112-
}
3099+
data_sink.write = [&](const char *d, size_t l) -> bool {
3100+
if (ok) {
3101+
data_available = l > 0;
3102+
offset += l;
31133103

3114-
if (!payload.empty()) {
3115-
// Emit chunked response header and footer for each chunk
3116-
auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n";
3117-
if (!write_data(strm, chunk.data(), chunk.size())) {
3104+
std::string payload;
3105+
if (compressor.compress(d, l, false,
3106+
[&](const char *data, size_t data_len) {
3107+
payload.append(data, data_len);
3108+
return true;
3109+
})) {
3110+
if (!payload.empty()) {
3111+
// Emit chunked response header and footer for each chunk
3112+
auto chunk =
3113+
from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n";
3114+
if (!write_data(strm, chunk.data(), chunk.size())) {
3115+
ok = false;
3116+
}
3117+
}
3118+
} else {
31183119
ok = false;
3119-
return;
31203120
}
31213121
}
3122+
return ok;
31223123
};
31233124

31243125
data_sink.done = [&](void) {
@@ -5747,7 +5748,7 @@ inline std::unique_ptr<Response> ClientImpl::send_with_content_provider(
57475748
size_t offset = 0;
57485749
DataSink data_sink;
57495750

5750-
data_sink.write = [&](const char *data, size_t data_len) {
5751+
data_sink.write = [&](const char *data, size_t data_len) -> bool {
57515752
if (ok) {
57525753
auto last = offset + data_len == content_length;
57535754

@@ -5763,6 +5764,7 @@ inline std::unique_ptr<Response> ClientImpl::send_with_content_provider(
57635764
ok = false;
57645765
}
57655766
}
5767+
return ok;
57665768
};
57675769

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

test/test.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,8 @@ class ServerTest : public ::testing::Test {
13661366
const auto &d = *data;
13671367
auto out_len =
13681368
std::min(static_cast<size_t>(length), DATA_CHUNK_SIZE);
1369-
sink.write(&d[static_cast<size_t>(offset)], out_len);
1369+
auto ret = sink.write(&d[static_cast<size_t>(offset)], out_len);
1370+
EXPECT_TRUE(ret);
13701371
return true;
13711372
},
13721373
[data] { delete data; });
@@ -2521,7 +2522,8 @@ TEST_F(ServerTest, SlowPost) {
25212522
auto res = cli_.Post(
25222523
"/slowpost", 64 * 1024 * 1024,
25232524
[&](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
2524-
sink.write(buffer, sizeof(buffer));
2525+
auto ret = sink.write(buffer, sizeof(buffer));
2526+
EXPECT_TRUE(ret);
25252527
return true;
25262528
},
25272529
"text/plain");
@@ -3349,7 +3351,8 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) {
33493351
DataSink &sink) {
33503352
char buffer[27];
33513353
auto size = static_cast<size_t>(sprintf(buffer, "data:%ld\n\n", offset));
3352-
sink.write(buffer, size);
3354+
auto ret = sink.write(buffer, size);
3355+
EXPECT_TRUE(ret);
33533356
std::this_thread::sleep_for(std::chrono::seconds(1));
33543357
return true;
33553358
});

0 commit comments

Comments
 (0)