1
0
mirror of synced 2025-04-20 11:47:43 +03:00

Code cleanup and format

This commit is contained in:
yhirose 2020-05-16 17:22:55 -04:00
parent f5598237b2
commit 29fd136afd
2 changed files with 31 additions and 32 deletions

View File

@ -2197,9 +2197,7 @@ inline ssize_t write_content_chunked(Stream &strm,
} }
} }
}; };
data_sink.is_writable = [&](void) { data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };
return ok && strm.is_writable();
};
while (data_available && !is_shutting_down()) { while (data_available && !is_shutting_down()) {
if (!content_provider(offset, 0, data_sink)) { return -1; } if (!content_provider(offset, 0, data_sink)) { return -1; }
@ -4197,9 +4195,7 @@ inline bool Client::write_request(Stream &strm, const Request &req,
// Flush buffer // Flush buffer
auto &data = bstrm.get_buffer(); auto &data = bstrm.get_buffer();
if (!detail::write_data(strm, data.data(), data.size())) { if (!detail::write_data(strm, data.data(), data.size())) { return false; }
return false;
}
// Body // Body
if (req.body.empty()) { if (req.body.empty()) {
@ -4219,9 +4215,7 @@ inline bool Client::write_request(Stream &strm, const Request &req,
} }
} }
}; };
data_sink.is_writable = [&](void) { data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };
return ok && strm.is_writable();
};
while (offset < end_offset) { while (offset < end_offset) {
if (!req.content_provider(offset, end_offset - offset, data_sink)) { if (!req.content_provider(offset, end_offset - offset, data_sink)) {

View File

@ -334,7 +334,7 @@ TEST(RangeTest, FromHTTPBin) {
httplib::Headers headers; httplib::Headers headers;
auto res = cli.Get("/range/32", headers); auto res = cli.Get("/range/32", headers);
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef"); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
} }
@ -342,7 +342,7 @@ TEST(RangeTest, FromHTTPBin) {
httplib::Headers headers = {httplib::make_range_header({{1, -1}})}; httplib::Headers headers = {httplib::make_range_header({{1, -1}})};
auto res = cli.Get("/range/32", headers); auto res = cli.Get("/range/32", headers);
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(res->body, "bcdefghijklmnopqrstuvwxyzabcdef"); EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(206, res->status); EXPECT_EQ(206, res->status);
} }
@ -350,7 +350,7 @@ TEST(RangeTest, FromHTTPBin) {
httplib::Headers headers = {httplib::make_range_header({{1, 10}})}; httplib::Headers headers = {httplib::make_range_header({{1, 10}})};
auto res = cli.Get("/range/32", headers); auto res = cli.Get("/range/32", headers);
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(res->body, "bcdefghijk"); EXPECT_EQ("bcdefghijk", res->body);
EXPECT_EQ(206, res->status); EXPECT_EQ(206, res->status);
} }
@ -358,7 +358,7 @@ TEST(RangeTest, FromHTTPBin) {
httplib::Headers headers = {httplib::make_range_header({{0, 31}})}; httplib::Headers headers = {httplib::make_range_header({{0, 31}})};
auto res = cli.Get("/range/32", headers); auto res = cli.Get("/range/32", headers);
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef"); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
} }
@ -366,7 +366,7 @@ TEST(RangeTest, FromHTTPBin) {
httplib::Headers headers = {httplib::make_range_header({{0, -1}})}; httplib::Headers headers = {httplib::make_range_header({{0, -1}})};
auto res = cli.Get("/range/32", headers); auto res = cli.Get("/range/32", headers);
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef"); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
} }
@ -440,7 +440,7 @@ TEST(CancelTest, NoCancel) {
auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return true; }); auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return true; });
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef"); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
} }
@ -501,8 +501,8 @@ TEST(BaseAuthTest, FromHTTPWatch) {
cli.Get("/basic-auth/hello/world", cli.Get("/basic-auth/hello/world",
{httplib::make_basic_authentication_header("hello", "world")}); {httplib::make_basic_authentication_header("hello", "world")});
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(res->body, EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
"{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n"); res->body);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
} }
@ -510,8 +510,8 @@ TEST(BaseAuthTest, FromHTTPWatch) {
cli.set_basic_auth("hello", "world"); cli.set_basic_auth("hello", "world");
auto res = cli.Get("/basic-auth/hello/world"); auto res = cli.Get("/basic-auth/hello/world");
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(res->body, EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
"{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n"); res->body);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
} }
@ -554,8 +554,8 @@ TEST(DigestAuthTest, FromHTTPWatch) {
for (auto path : paths) { for (auto path : paths) {
auto res = cli.Get(path.c_str()); auto res = cli.Get(path.c_str());
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(res->body, EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
"{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n"); res->body);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
} }
@ -689,7 +689,7 @@ TEST(RedirectToDifferentPort, Redirect) {
auto res = cli.Get("/1"); auto res = cli.Get("/1");
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
EXPECT_EQ(res->body, "Hello World!"); EXPECT_EQ("Hello World!", res->body);
svr8080.stop(); svr8080.stop();
svr8081.stop(); svr8081.stop();
@ -718,7 +718,7 @@ TEST(Server, BindDualStack) {
auto res = cli.Get("/1"); auto res = cli.Get("/1");
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
EXPECT_EQ(res->body, "Hello World!"); EXPECT_EQ("Hello World!", res->body);
} }
{ {
Client cli("::1", PORT); Client cli("::1", PORT);
@ -726,7 +726,7 @@ TEST(Server, BindDualStack) {
auto res = cli.Get("/1"); auto res = cli.Get("/1");
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
EXPECT_EQ(res->body, "Hello World!"); EXPECT_EQ("Hello World!", res->body);
} }
svr.stop(); svr.stop();
thread.join(); thread.join();
@ -1893,14 +1893,18 @@ TEST_F(ServerTest, SlowRequest) {
TEST_F(ServerTest, SlowPost) { TEST_F(ServerTest, SlowPost) {
char buffer[64 * 1024]; char buffer[64 * 1024];
memset(buffer, 0x42, sizeof(buffer)); memset(buffer, 0x42, sizeof(buffer));
auto res = cli_.Post( auto res = cli_.Post(
"/slowpost", 64*1024*1024, "/slowpost", 64 * 1024 * 1024,
[&] (size_t /*offset*/, size_t /*length*/, DataSink & sink) { [&](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
sink.write(buffer, sizeof(buffer)); return true; sink.write(buffer, sizeof(buffer));
return true;
}, },
"text/plain"); "text/plain");
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
cli_.set_write_timeout(0, 0); cli_.set_write_timeout(0, 0);
res = cli_.Post( res = cli_.Post(
"/slowpost", 64 * 1024 * 1024, "/slowpost", 64 * 1024 * 1024,
@ -1909,6 +1913,7 @@ TEST_F(ServerTest, SlowPost) {
return true; return true;
}, },
"text/plain"); "text/plain");
ASSERT_FALSE(res != nullptr); ASSERT_FALSE(res != nullptr);
} }
@ -2465,7 +2470,7 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) {
svr.Get("/events", [](const Request & /*req*/, Response &res) { svr.Get("/events", [](const Request & /*req*/, Response &res) {
res.set_header("Content-Type", "text/event-stream"); res.set_header("Content-Type", "text/event-stream");
res.set_header("Cache-Control", "no-cache"); res.set_header("Cache-Control", "no-cache");
res.set_chunked_content_provider([](size_t offset, const DataSink &sink) { res.set_chunked_content_provider([](size_t offset, DataSink &sink) {
char buffer[27]; char buffer[27];
auto size = static_cast<size_t>(sprintf(buffer, "data:%ld\n\n", offset)); auto size = static_cast<size_t>(sprintf(buffer, "data:%ld\n\n", offset));
sink.write(buffer, size); sink.write(buffer, size);