Code format

This commit is contained in:
yhirose 2021-04-13 20:52:49 -04:00
parent 6cc2edce99
commit 63643e6386
2 changed files with 103 additions and 89 deletions

View file

@ -2065,8 +2065,9 @@ inline int shutdown_socket(socket_t sock) {
} }
template <typename BindOrConnect> template <typename BindOrConnect>
socket_t create_socket(const char *host, int port, int address_family, int socket_flags, socket_t create_socket(const char *host, int port, int address_family,
bool tcp_nodelay, SocketOptions socket_options, int socket_flags, bool tcp_nodelay,
SocketOptions socket_options,
BindOrConnect bind_or_connect) { BindOrConnect bind_or_connect) {
// Get address info // Get address info
struct addrinfo hints; struct addrinfo hints;
@ -2214,8 +2215,7 @@ inline std::string if2ip(const std::string &ifn) {
#endif #endif
inline socket_t create_client_socket(const char *host, int port, inline socket_t create_client_socket(const char *host, int port,
int address_family, int address_family, bool tcp_nodelay,
bool tcp_nodelay,
SocketOptions socket_options, SocketOptions socket_options,
time_t timeout_sec, time_t timeout_usec, time_t timeout_sec, time_t timeout_usec,
const std::string &intf, Error &error) { const std::string &intf, Error &error) {
@ -4771,7 +4771,8 @@ inline socket_t
Server::create_server_socket(const char *host, int port, int socket_flags, Server::create_server_socket(const char *host, int port, int socket_flags,
SocketOptions socket_options) const { SocketOptions socket_options) const {
return detail::create_socket( return detail::create_socket(
host, port, address_family_, socket_flags, tcp_nodelay_, std::move(socket_options), host, port, address_family_, socket_flags, tcp_nodelay_,
std::move(socket_options),
[](socket_t sock, struct addrinfo &ai) -> bool { [](socket_t sock, struct addrinfo &ai) -> bool {
if (::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) { if (::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
return false; return false;
@ -5260,8 +5261,9 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) {
inline socket_t ClientImpl::create_client_socket(Error &error) const { inline socket_t ClientImpl::create_client_socket(Error &error) const {
if (!proxy_host_.empty() && proxy_port_ != -1) { if (!proxy_host_.empty() && proxy_port_ != -1) {
return detail::create_client_socket( return detail::create_client_socket(
proxy_host_.c_str(), proxy_port_, address_family_, tcp_nodelay_, socket_options_, proxy_host_.c_str(), proxy_port_, address_family_, tcp_nodelay_,
connection_timeout_sec_, connection_timeout_usec_, interface_, error); socket_options_, connection_timeout_sec_, connection_timeout_usec_,
interface_, error);
} }
return detail::create_client_socket( return detail::create_client_socket(
host_.c_str(), port_, address_family_, tcp_nodelay_, socket_options_, host_.c_str(), port_, address_family_, tcp_nodelay_, socket_options_,
@ -6392,7 +6394,9 @@ inline void ClientImpl::set_default_headers(Headers headers) {
default_headers_ = std::move(headers); default_headers_ = std::move(headers);
} }
inline void ClientImpl::set_address_family(int family) { address_family_ = family; } inline void ClientImpl::set_address_family(int family) {
address_family_ = family;
}
inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; } inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; }
@ -7456,7 +7460,9 @@ inline void Client::set_default_headers(Headers headers) {
cli_->set_default_headers(std::move(headers)); cli_->set_default_headers(std::move(headers));
} }
inline void Client::set_address_family(int family) { cli_->set_address_family(family); } inline void Client::set_address_family(int family) {
cli_->set_address_family(family);
}
inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); }

View file

@ -417,16 +417,16 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) {
cli.set_connection_timeout(2); cli.set_connection_timeout(2);
std::string body; std::string body;
auto res = auto res = cli.Get(
cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137", "/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
[&](const Response &response) { [&](const Response &response) {
EXPECT_EQ(200, response.status); EXPECT_EQ(200, response.status);
return true; return true;
}, },
[&](const char *data, size_t data_length) { [&](const char *data, size_t data_length) {
body.append(data, data_length); body.append(data, data_length);
return true; return true;
}); });
ASSERT_TRUE(res); ASSERT_TRUE(res);
std::string out; std::string out;
@ -916,11 +916,10 @@ TEST(RedirectFromPageWithContent, Redirect) {
cli.set_follow_location(true); cli.set_follow_location(true);
std::string body; std::string body;
auto res = cli.Get("/1", auto res = cli.Get("/1", [&](const char *data, size_t data_length) {
[&](const char *data, size_t data_length) { body.append(data, data_length);
body.append(data, data_length); return true;
return true; });
});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
@ -931,11 +930,10 @@ TEST(RedirectFromPageWithContent, Redirect) {
Client cli("localhost", PORT); Client cli("localhost", PORT);
std::string body; std::string body;
auto res = cli.Get("/1", auto res = cli.Get("/1", [&](const char *data, size_t data_length) {
[&](const char *data, size_t data_length) { body.append(data, data_length);
body.append(data, data_length); return true;
return true; });
});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(302, res->status); EXPECT_EQ(302, res->status);
@ -2520,13 +2518,13 @@ TEST_F(ServerTest, SlowPost) {
char buffer[64 * 1024]; char buffer[64 * 1024];
memset(buffer, 0x42, sizeof(buffer)); memset(buffer, 0x42, sizeof(buffer));
auto res = auto res = cli_.Post(
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)); sink.write(buffer, sizeof(buffer));
return true; return true;
}, },
"text/plain"); "text/plain");
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
@ -2537,13 +2535,13 @@ TEST_F(ServerTest, SlowPostFail) {
memset(buffer, 0x42, sizeof(buffer)); memset(buffer, 0x42, sizeof(buffer));
cli_.set_write_timeout(std::chrono::seconds(0)); cli_.set_write_timeout(std::chrono::seconds(0));
auto res = auto res = cli_.Post(
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)); sink.write(buffer, sizeof(buffer));
return true; return true;
}, },
"text/plain"); "text/plain");
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Write, res.error()); EXPECT_EQ(Error::Write, res.error());
@ -2557,13 +2555,14 @@ TEST_F(ServerTest, Put) {
} }
TEST_F(ServerTest, PutWithContentProvider) { TEST_F(ServerTest, PutWithContentProvider) {
auto res = cli_.Put("/put", 3, auto res = cli_.Put(
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) { "/put", 3,
EXPECT_TRUE(sink.is_writable()); [](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
sink.os << "PUT"; EXPECT_TRUE(sink.is_writable());
return true; sink.os << "PUT";
}, return true;
"text/plain"); },
"text/plain");
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
@ -2571,24 +2570,27 @@ TEST_F(ServerTest, PutWithContentProvider) {
} }
TEST_F(ServerTest, PostWithContentProviderAbort) { TEST_F(ServerTest, PostWithContentProviderAbort) {
auto res = cli_.Post("/post", 42, auto res = cli_.Post(
[](size_t /*offset*/, size_t /*length*/, "/post", 42,
DataSink & /*sink*/) { return false; }, [](size_t /*offset*/, size_t /*length*/, DataSink & /*sink*/) {
"text/plain"); return false;
},
"text/plain");
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
TEST_F(ServerTest, PutWithContentProviderWithoutLength) { TEST_F(ServerTest, PutWithContentProviderWithoutLength) {
auto res = cli_.Put("/put", auto res = cli_.Put(
[](size_t /*offset*/, DataSink &sink) { "/put",
EXPECT_TRUE(sink.is_writable()); [](size_t /*offset*/, DataSink &sink) {
sink.os << "PUT"; EXPECT_TRUE(sink.is_writable());
sink.done(); sink.os << "PUT";
return true; sink.done();
}, return true;
"text/plain"); },
"text/plain");
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
@ -2607,13 +2609,14 @@ TEST_F(ServerTest, PostWithContentProviderWithoutLengthAbort) {
#ifdef CPPHTTPLIB_ZLIB_SUPPORT #ifdef CPPHTTPLIB_ZLIB_SUPPORT
TEST_F(ServerTest, PutWithContentProviderWithGzip) { TEST_F(ServerTest, PutWithContentProviderWithGzip) {
cli_.set_compress(true); cli_.set_compress(true);
auto res = cli_.Put("/put", 3, auto res = cli_.Put(
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) { "/put", 3,
EXPECT_TRUE(sink.is_writable()); [](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
sink.os << "PUT"; EXPECT_TRUE(sink.is_writable());
return true; sink.os << "PUT";
}, return true;
"text/plain"); },
"text/plain");
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
@ -2622,10 +2625,12 @@ TEST_F(ServerTest, PutWithContentProviderWithGzip) {
TEST_F(ServerTest, PostWithContentProviderWithGzipAbort) { TEST_F(ServerTest, PostWithContentProviderWithGzipAbort) {
cli_.set_compress(true); cli_.set_compress(true);
auto res = cli_.Post("/post", 42, auto res = cli_.Post(
[](size_t /*offset*/, size_t /*length*/, "/post", 42,
DataSink & /*sink*/) { return false; }, [](size_t /*offset*/, size_t /*length*/, DataSink & /*sink*/) {
"text/plain"); return false;
},
"text/plain");
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
@ -2633,14 +2638,15 @@ TEST_F(ServerTest, PostWithContentProviderWithGzipAbort) {
TEST_F(ServerTest, PutWithContentProviderWithoutLengthWithGzip) { TEST_F(ServerTest, PutWithContentProviderWithoutLengthWithGzip) {
cli_.set_compress(true); cli_.set_compress(true);
auto res = cli_.Put("/put", auto res = cli_.Put(
[](size_t /*offset*/, DataSink &sink) { "/put",
EXPECT_TRUE(sink.is_writable()); [](size_t /*offset*/, DataSink &sink) {
sink.os << "PUT"; EXPECT_TRUE(sink.is_writable());
sink.done(); sink.os << "PUT";
return true; sink.done();
}, return true;
"text/plain"); },
"text/plain");
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
@ -2960,8 +2966,9 @@ TEST_F(ServerTest, KeepAlive) {
EXPECT_EQ("empty", res->body); EXPECT_EQ("empty", res->body);
EXPECT_EQ("close", res->get_header_value("Connection")); EXPECT_EQ("close", res->get_header_value("Connection"));
res = cli_.Post("/empty", 0, [&](size_t, size_t, DataSink &) { return true; }, res = cli_.Post(
"text/plain"); "/empty", 0, [&](size_t, size_t, DataSink &) { return true; },
"text/plain");
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
@ -3556,7 +3563,7 @@ TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) {
TEST(GetWithParametersTest, GetWithParameters) { TEST(GetWithParametersTest, GetWithParameters) {
Server svr; Server svr;
svr.Get("/", [&](const Request & req, Response &res) { svr.Get("/", [&](const Request &req, Response &res) {
auto text = req.get_param_value("hello"); auto text = req.get_param_value("hello");
res.set_content(text, "text/plain"); res.set_content(text, "text/plain");
}); });
@ -3585,7 +3592,7 @@ TEST(GetWithParametersTest, GetWithParameters) {
TEST(GetWithParametersTest, GetWithParameters2) { TEST(GetWithParametersTest, GetWithParameters2) {
Server svr; Server svr;
svr.Get("/", [&](const Request & req, Response &res) { svr.Get("/", [&](const Request &req, Response &res) {
auto text = req.get_param_value("hello"); auto text = req.get_param_value("hello");
res.set_content(text, "text/plain"); res.set_content(text, "text/plain");
}); });
@ -3602,10 +3609,11 @@ TEST(GetWithParametersTest, GetWithParameters2) {
params.emplace("hello", "world"); params.emplace("hello", "world");
std::string body; std::string body;
auto res = cli.Get("/", params, Headers{}, [&](const char *data, size_t data_length) { auto res = cli.Get("/", params, Headers{},
body.append(data, data_length); [&](const char *data, size_t data_length) {
return true; body.append(data, data_length);
}); return true;
});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);