PostHTTP: try IPv6, then IPv4 on failure to connect()

This commit is contained in:
Lion Kortlepel 2021-01-30 01:30:22 +01:00
parent 33a7d0e1a1
commit d47e721b38

View File

@ -82,19 +82,31 @@ std::string PostHTTP(const std::string& host, const std::string& target, const s
tcp::resolver resolver(io);
beast::ssl_stream<beast::tcp_stream> stream(io, ctx);
decltype(resolver)::results_type results;
auto try_connect_with_protocol = [&](tcp protocol) {
try {
results = resolver.resolve(tcp::v6(), host, std::to_string(443));
} catch (const boost::system::system_error&) {
error("ipv6 not supported!");
}
results = resolver.resolve(protocol, host, std::to_string(443));
if (!SSL_set_tlsext_host_name(stream.native_handle(), host.c_str())) {
boost::system::error_code ec { static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category() };
// FIXME: we could throw and crash, if we like
// throw boost::system::system_error { ec };
debug("POST " + host + target + " failed.");
return "-1";
return false;
}
beast::get_lowest_layer(stream).connect(results);
} catch (const boost::system::system_error&) {
return false;
}
return true;
};
bool ok = try_connect_with_protocol(tcp::v6());
if (!ok) {
debug("IPv6 connect failed, trying IPv4");
ok = try_connect_with_protocol(tcp::v4());
if (!ok) {
error("failed to resolve or connect in POST " + host + target);
return "-1";
}
}
stream.handshake(ssl::stream_base::client);
http::request<http::string_body> req { http::verb::post, target, 11 /* http 1.1 */ };