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

Added set_address_family

This commit is contained in:
yhirose 2021-04-13 20:49:52 -04:00
parent d122ff3ca8
commit 6cc2edce99
2 changed files with 22 additions and 7 deletions

View File

@ -663,6 +663,7 @@ public:
Server &set_expect_100_continue_handler(Expect100ContinueHandler handler);
Server &set_logger(Logger logger);
Server &set_address_family(int family);
Server &set_tcp_nodelay(bool on);
Server &set_socket_options(SocketOptions socket_options);
@ -782,6 +783,7 @@ private:
Logger logger_;
Expect100ContinueHandler expect_100_continue_handler_;
int address_family_ = AF_UNSPEC;
bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY;
SocketOptions socket_options_ = default_socket_options;
};
@ -971,6 +973,7 @@ public:
void set_default_headers(Headers headers);
void set_address_family(int family);
void set_tcp_nodelay(bool on);
void set_socket_options(SocketOptions socket_options);
@ -1092,6 +1095,7 @@ protected:
bool keep_alive_ = false;
bool follow_location_ = false;
int address_family_ = AF_UNSPEC;
bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY;
SocketOptions socket_options_ = nullptr;
@ -1282,6 +1286,7 @@ public:
void set_default_headers(Headers headers);
void set_address_family(int family);
void set_tcp_nodelay(bool on);
void set_socket_options(SocketOptions socket_options);
@ -2060,7 +2065,7 @@ inline int shutdown_socket(socket_t sock) {
}
template <typename BindOrConnect>
socket_t create_socket(const char *host, int port, int socket_flags,
socket_t create_socket(const char *host, int port, int address_family, int socket_flags,
bool tcp_nodelay, SocketOptions socket_options,
BindOrConnect bind_or_connect) {
// Get address info
@ -2068,7 +2073,7 @@ socket_t create_socket(const char *host, int port, int socket_flags,
struct addrinfo *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_family = address_family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = socket_flags;
hints.ai_protocol = 0;
@ -2209,12 +2214,13 @@ inline std::string if2ip(const std::string &ifn) {
#endif
inline socket_t create_client_socket(const char *host, int port,
int address_family,
bool tcp_nodelay,
SocketOptions socket_options,
time_t timeout_sec, time_t timeout_usec,
const std::string &intf, Error &error) {
auto sock = create_socket(
host, port, 0, tcp_nodelay, std::move(socket_options),
host, port, address_family, 0, tcp_nodelay, std::move(socket_options),
[&](socket_t sock, struct addrinfo &ai) -> bool {
if (!intf.empty()) {
#ifdef USE_IF2IP
@ -4381,6 +4387,11 @@ Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
return *this;
}
inline Server &Server::set_address_family(int family) {
address_family_ = family;
return *this;
}
inline Server &Server::set_tcp_nodelay(bool on) {
tcp_nodelay_ = on;
return *this;
@ -4760,7 +4771,7 @@ inline socket_t
Server::create_server_socket(const char *host, int port, int socket_flags,
SocketOptions socket_options) const {
return detail::create_socket(
host, port, 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 {
if (::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
return false;
@ -5249,11 +5260,11 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) {
inline socket_t ClientImpl::create_client_socket(Error &error) const {
if (!proxy_host_.empty() && proxy_port_ != -1) {
return detail::create_client_socket(
proxy_host_.c_str(), proxy_port_, tcp_nodelay_, socket_options_,
proxy_host_.c_str(), proxy_port_, address_family_, tcp_nodelay_, socket_options_,
connection_timeout_sec_, connection_timeout_usec_, interface_, error);
}
return detail::create_client_socket(
host_.c_str(), port_, tcp_nodelay_, socket_options_,
host_.c_str(), port_, address_family_, tcp_nodelay_, socket_options_,
connection_timeout_sec_, connection_timeout_usec_, interface_, error);
}
@ -6381,6 +6392,8 @@ inline void ClientImpl::set_default_headers(Headers headers) {
default_headers_ = std::move(headers);
}
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_socket_options(SocketOptions socket_options) {
@ -7443,6 +7456,8 @@ inline void Client::set_default_headers(Headers 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_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); }
inline void Client::set_socket_options(SocketOptions socket_options) {

View File

@ -3137,7 +3137,7 @@ static bool send_request(time_t read_timeout_sec, const std::string &req,
auto error = Error::Success;
auto client_sock =
detail::create_client_socket(HOST, PORT, false, nullptr,
detail::create_client_socket(HOST, PORT, AF_UNSPEC, false, nullptr,
/*timeout_sec=*/5, 0, std::string(), error);
if (client_sock == INVALID_SOCKET) { return false; }