@@ -663,6 +663,7 @@ class Server {
663
663
Server &set_expect_100_continue_handler (Expect100ContinueHandler handler);
664
664
Server &set_logger (Logger logger);
665
665
666
+ Server &set_address_family (int family);
666
667
Server &set_tcp_nodelay (bool on);
667
668
Server &set_socket_options (SocketOptions socket_options);
668
669
@@ -782,6 +783,7 @@ class Server {
782
783
Logger logger_;
783
784
Expect100ContinueHandler expect_100_continue_handler_;
784
785
786
+ int address_family_ = AF_UNSPEC;
785
787
bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY;
786
788
SocketOptions socket_options_ = default_socket_options;
787
789
};
@@ -971,6 +973,7 @@ class ClientImpl {
971
973
972
974
void set_default_headers (Headers headers);
973
975
976
+ void set_address_family (int family);
974
977
void set_tcp_nodelay (bool on);
975
978
void set_socket_options (SocketOptions socket_options);
976
979
@@ -1092,6 +1095,7 @@ class ClientImpl {
1092
1095
bool keep_alive_ = false ;
1093
1096
bool follow_location_ = false ;
1094
1097
1098
+ int address_family_ = AF_UNSPEC;
1095
1099
bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY;
1096
1100
SocketOptions socket_options_ = nullptr ;
1097
1101
@@ -1282,6 +1286,7 @@ class Client {
1282
1286
1283
1287
void set_default_headers (Headers headers);
1284
1288
1289
+ void set_address_family (int family);
1285
1290
void set_tcp_nodelay (bool on);
1286
1291
void set_socket_options (SocketOptions socket_options);
1287
1292
@@ -2060,15 +2065,15 @@ inline int shutdown_socket(socket_t sock) {
2060
2065
}
2061
2066
2062
2067
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,
2064
2069
bool tcp_nodelay, SocketOptions socket_options,
2065
2070
BindOrConnect bind_or_connect) {
2066
2071
// Get address info
2067
2072
struct addrinfo hints;
2068
2073
struct addrinfo *result;
2069
2074
2070
2075
memset (&hints, 0 , sizeof (struct addrinfo ));
2071
- hints.ai_family = AF_UNSPEC ;
2076
+ hints.ai_family = address_family ;
2072
2077
hints.ai_socktype = SOCK_STREAM;
2073
2078
hints.ai_flags = socket_flags;
2074
2079
hints.ai_protocol = 0 ;
@@ -2209,12 +2214,13 @@ inline std::string if2ip(const std::string &ifn) {
2209
2214
#endif
2210
2215
2211
2216
inline socket_t create_client_socket (const char *host, int port,
2217
+ int address_family,
2212
2218
bool tcp_nodelay,
2213
2219
SocketOptions socket_options,
2214
2220
time_t timeout_sec, time_t timeout_usec,
2215
2221
const std::string &intf, Error &error) {
2216
2222
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),
2218
2224
[&](socket_t sock, struct addrinfo &ai) -> bool {
2219
2225
if (!intf.empty ()) {
2220
2226
#ifdef USE_IF2IP
@@ -4381,6 +4387,11 @@ Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
4381
4387
return *this ;
4382
4388
}
4383
4389
4390
+ inline Server &Server::set_address_family (int family) {
4391
+ address_family_ = family;
4392
+ return *this ;
4393
+ }
4394
+
4384
4395
inline Server &Server::set_tcp_nodelay (bool on) {
4385
4396
tcp_nodelay_ = on;
4386
4397
return *this ;
@@ -4760,7 +4771,7 @@ inline socket_t
4760
4771
Server::create_server_socket (const char *host, int port, int socket_flags,
4761
4772
SocketOptions socket_options) const {
4762
4773
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),
4764
4775
[](socket_t sock, struct addrinfo &ai) -> bool {
4765
4776
if (::bind (sock, ai.ai_addr , static_cast <socklen_t >(ai.ai_addrlen ))) {
4766
4777
return false ;
@@ -5249,11 +5260,11 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) {
5249
5260
inline socket_t ClientImpl::create_client_socket (Error &error) const {
5250
5261
if (!proxy_host_.empty () && proxy_port_ != -1 ) {
5251
5262
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_,
5253
5264
connection_timeout_sec_, connection_timeout_usec_, interface_, error);
5254
5265
}
5255
5266
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_,
5257
5268
connection_timeout_sec_, connection_timeout_usec_, interface_, error);
5258
5269
}
5259
5270
@@ -6381,6 +6392,8 @@ inline void ClientImpl::set_default_headers(Headers headers) {
6381
6392
default_headers_ = std::move (headers);
6382
6393
}
6383
6394
6395
+ inline void ClientImpl::set_address_family (int family) { address_family_ = family; }
6396
+
6384
6397
inline void ClientImpl::set_tcp_nodelay (bool on) { tcp_nodelay_ = on; }
6385
6398
6386
6399
inline void ClientImpl::set_socket_options (SocketOptions socket_options) {
@@ -7443,6 +7456,8 @@ inline void Client::set_default_headers(Headers headers) {
7443
7456
cli_->set_default_headers (std::move (headers));
7444
7457
}
7445
7458
7459
+ inline void Client::set_address_family (int family) { cli_->set_address_family (family); }
7460
+
7446
7461
inline void Client::set_tcp_nodelay (bool on) { cli_->set_tcp_nodelay (on); }
7447
7462
7448
7463
inline void Client::set_socket_options (SocketOptions socket_options) {
0 commit comments