Skip to content

Commit 6cc2edc

Browse files
committed
Added set_address_family
1 parent d122ff3 commit 6cc2edc

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

httplib.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ class Server {
663663
Server &set_expect_100_continue_handler(Expect100ContinueHandler handler);
664664
Server &set_logger(Logger logger);
665665

666+
Server &set_address_family(int family);
666667
Server &set_tcp_nodelay(bool on);
667668
Server &set_socket_options(SocketOptions socket_options);
668669

@@ -782,6 +783,7 @@ class Server {
782783
Logger logger_;
783784
Expect100ContinueHandler expect_100_continue_handler_;
784785

786+
int address_family_ = AF_UNSPEC;
785787
bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY;
786788
SocketOptions socket_options_ = default_socket_options;
787789
};
@@ -971,6 +973,7 @@ class ClientImpl {
971973

972974
void set_default_headers(Headers headers);
973975

976+
void set_address_family(int family);
974977
void set_tcp_nodelay(bool on);
975978
void set_socket_options(SocketOptions socket_options);
976979

@@ -1092,6 +1095,7 @@ class ClientImpl {
10921095
bool keep_alive_ = false;
10931096
bool follow_location_ = false;
10941097

1098+
int address_family_ = AF_UNSPEC;
10951099
bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY;
10961100
SocketOptions socket_options_ = nullptr;
10971101

@@ -1282,6 +1286,7 @@ class Client {
12821286

12831287
void set_default_headers(Headers headers);
12841288

1289+
void set_address_family(int family);
12851290
void set_tcp_nodelay(bool on);
12861291
void set_socket_options(SocketOptions socket_options);
12871292

@@ -2060,15 +2065,15 @@ inline int shutdown_socket(socket_t sock) {
20602065
}
20612066

20622067
template <typename BindOrConnect>
2063-
socket_t create_socket(const char *host, int port, int socket_flags,
2068+
socket_t create_socket(const char *host, int port, int address_family, int socket_flags,
20642069
bool tcp_nodelay, SocketOptions socket_options,
20652070
BindOrConnect bind_or_connect) {
20662071
// Get address info
20672072
struct addrinfo hints;
20682073
struct addrinfo *result;
20692074

20702075
memset(&hints, 0, sizeof(struct addrinfo));
2071-
hints.ai_family = AF_UNSPEC;
2076+
hints.ai_family = address_family;
20722077
hints.ai_socktype = SOCK_STREAM;
20732078
hints.ai_flags = socket_flags;
20742079
hints.ai_protocol = 0;
@@ -2209,12 +2214,13 @@ inline std::string if2ip(const std::string &ifn) {
22092214
#endif
22102215

22112216
inline socket_t create_client_socket(const char *host, int port,
2217+
int address_family,
22122218
bool tcp_nodelay,
22132219
SocketOptions socket_options,
22142220
time_t timeout_sec, time_t timeout_usec,
22152221
const std::string &intf, Error &error) {
22162222
auto sock = create_socket(
2217-
host, port, 0, tcp_nodelay, std::move(socket_options),
2223+
host, port, address_family, 0, tcp_nodelay, std::move(socket_options),
22182224
[&](socket_t sock, struct addrinfo &ai) -> bool {
22192225
if (!intf.empty()) {
22202226
#ifdef USE_IF2IP
@@ -4381,6 +4387,11 @@ Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
43814387
return *this;
43824388
}
43834389

4390+
inline Server &Server::set_address_family(int family) {
4391+
address_family_ = family;
4392+
return *this;
4393+
}
4394+
43844395
inline Server &Server::set_tcp_nodelay(bool on) {
43854396
tcp_nodelay_ = on;
43864397
return *this;
@@ -4760,7 +4771,7 @@ inline socket_t
47604771
Server::create_server_socket(const char *host, int port, int socket_flags,
47614772
SocketOptions socket_options) const {
47624773
return detail::create_socket(
4763-
host, port, socket_flags, tcp_nodelay_, std::move(socket_options),
4774+
host, port, address_family_, socket_flags, tcp_nodelay_, std::move(socket_options),
47644775
[](socket_t sock, struct addrinfo &ai) -> bool {
47654776
if (::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
47664777
return false;
@@ -5249,11 +5260,11 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) {
52495260
inline socket_t ClientImpl::create_client_socket(Error &error) const {
52505261
if (!proxy_host_.empty() && proxy_port_ != -1) {
52515262
return detail::create_client_socket(
5252-
proxy_host_.c_str(), proxy_port_, tcp_nodelay_, socket_options_,
5263+
proxy_host_.c_str(), proxy_port_, address_family_, tcp_nodelay_, socket_options_,
52535264
connection_timeout_sec_, connection_timeout_usec_, interface_, error);
52545265
}
52555266
return detail::create_client_socket(
5256-
host_.c_str(), port_, tcp_nodelay_, socket_options_,
5267+
host_.c_str(), port_, address_family_, tcp_nodelay_, socket_options_,
52575268
connection_timeout_sec_, connection_timeout_usec_, interface_, error);
52585269
}
52595270

@@ -6381,6 +6392,8 @@ inline void ClientImpl::set_default_headers(Headers headers) {
63816392
default_headers_ = std::move(headers);
63826393
}
63836394

6395+
inline void ClientImpl::set_address_family(int family) { address_family_ = family; }
6396+
63846397
inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; }
63856398

63866399
inline void ClientImpl::set_socket_options(SocketOptions socket_options) {
@@ -7443,6 +7456,8 @@ inline void Client::set_default_headers(Headers headers) {
74437456
cli_->set_default_headers(std::move(headers));
74447457
}
74457458

7459+
inline void Client::set_address_family(int family) { cli_->set_address_family(family); }
7460+
74467461
inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); }
74477462

74487463
inline void Client::set_socket_options(SocketOptions socket_options) {

test/test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3137,7 +3137,7 @@ static bool send_request(time_t read_timeout_sec, const std::string &req,
31373137
auto error = Error::Success;
31383138

31393139
auto client_sock =
3140-
detail::create_client_socket(HOST, PORT, false, nullptr,
3140+
detail::create_client_socket(HOST, PORT, AF_UNSPEC, false, nullptr,
31413141
/*timeout_sec=*/5, 0, std::string(), error);
31423142

31433143
if (client_sock == INVALID_SOCKET) { return false; }

0 commit comments

Comments
 (0)