Code cleanup
This commit is contained in:
parent
dbd2465b56
commit
82fcbe3901
22
httplib.h
22
httplib.h
@ -3374,6 +3374,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
|
|||||||
#endif
|
#endif
|
||||||
return INVALID_SOCKET;
|
return INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
auto se = detail::scope_exit([&] { freeaddrinfo(result); });
|
||||||
|
|
||||||
for (auto rp = result; rp; rp = rp->ai_next) {
|
for (auto rp = result; rp; rp = rp->ai_next) {
|
||||||
// Create a socket
|
// Create a socket
|
||||||
@ -3443,17 +3444,13 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
|
|||||||
|
|
||||||
// bind or connect
|
// bind or connect
|
||||||
auto quit = false;
|
auto quit = false;
|
||||||
if (bind_or_connect(sock, *rp, quit)) {
|
if (bind_or_connect(sock, *rp, quit)) { return sock; }
|
||||||
freeaddrinfo(result);
|
|
||||||
return sock;
|
|
||||||
}
|
|
||||||
|
|
||||||
close_socket(sock);
|
close_socket(sock);
|
||||||
|
|
||||||
if (quit) { break; }
|
if (quit) { break; }
|
||||||
}
|
}
|
||||||
|
|
||||||
freeaddrinfo(result);
|
|
||||||
return INVALID_SOCKET;
|
return INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3486,6 +3483,7 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) {
|
|||||||
hints.ai_protocol = 0;
|
hints.ai_protocol = 0;
|
||||||
|
|
||||||
if (getaddrinfo(host.c_str(), "0", &hints, &result)) { return false; }
|
if (getaddrinfo(host.c_str(), "0", &hints, &result)) { return false; }
|
||||||
|
auto se = detail::scope_exit([&] { freeaddrinfo(result); });
|
||||||
|
|
||||||
auto ret = false;
|
auto ret = false;
|
||||||
for (auto rp = result; rp; rp = rp->ai_next) {
|
for (auto rp = result; rp; rp = rp->ai_next) {
|
||||||
@ -3496,7 +3494,6 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
freeaddrinfo(result);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3508,6 +3505,8 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) {
|
|||||||
inline std::string if2ip(int address_family, const std::string &ifn) {
|
inline std::string if2ip(int address_family, const std::string &ifn) {
|
||||||
struct ifaddrs *ifap;
|
struct ifaddrs *ifap;
|
||||||
getifaddrs(&ifap);
|
getifaddrs(&ifap);
|
||||||
|
auto se = detail::scope_exit([&] { freeifaddrs(ifap); });
|
||||||
|
|
||||||
std::string addr_candidate;
|
std::string addr_candidate;
|
||||||
for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
||||||
if (ifa->ifa_addr && ifn == ifa->ifa_name &&
|
if (ifa->ifa_addr && ifn == ifa->ifa_name &&
|
||||||
@ -3517,7 +3516,6 @@ inline std::string if2ip(int address_family, const std::string &ifn) {
|
|||||||
auto sa = reinterpret_cast<struct sockaddr_in *>(ifa->ifa_addr);
|
auto sa = reinterpret_cast<struct sockaddr_in *>(ifa->ifa_addr);
|
||||||
char buf[INET_ADDRSTRLEN];
|
char buf[INET_ADDRSTRLEN];
|
||||||
if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) {
|
if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) {
|
||||||
freeifaddrs(ifap);
|
|
||||||
return std::string(buf, INET_ADDRSTRLEN);
|
return std::string(buf, INET_ADDRSTRLEN);
|
||||||
}
|
}
|
||||||
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
|
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
|
||||||
@ -3530,7 +3528,6 @@ inline std::string if2ip(int address_family, const std::string &ifn) {
|
|||||||
if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) {
|
if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) {
|
||||||
addr_candidate = std::string(buf, INET6_ADDRSTRLEN);
|
addr_candidate = std::string(buf, INET6_ADDRSTRLEN);
|
||||||
} else {
|
} else {
|
||||||
freeifaddrs(ifap);
|
|
||||||
return std::string(buf, INET6_ADDRSTRLEN);
|
return std::string(buf, INET6_ADDRSTRLEN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3538,7 +3535,6 @@ inline std::string if2ip(int address_family, const std::string &ifn) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
freeifaddrs(ifap);
|
|
||||||
return addr_candidate;
|
return addr_candidate;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -5539,6 +5535,7 @@ inline void hosted_at(const std::string &hostname,
|
|||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
auto se = detail::scope_exit([&] { freeaddrinfo(result); });
|
||||||
|
|
||||||
for (auto rp = result; rp; rp = rp->ai_next) {
|
for (auto rp = result; rp; rp = rp->ai_next) {
|
||||||
const auto &addr =
|
const auto &addr =
|
||||||
@ -5550,8 +5547,6 @@ inline void hosted_at(const std::string &hostname,
|
|||||||
addrs.push_back(ip);
|
addrs.push_back(ip);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
freeaddrinfo(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string append_query_params(const std::string &path,
|
inline std::string append_query_params(const std::string &path,
|
||||||
@ -7223,7 +7218,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) {
|
|||||||
#endif
|
#endif
|
||||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
server_certificate_verification_ = rhs.server_certificate_verification_;
|
server_certificate_verification_ = rhs.server_certificate_verification_;
|
||||||
server_host_verification_ = rhs.server_host_verification_;
|
server_hostname_verification_ = rhs.server_hostname_verification_;
|
||||||
#endif
|
#endif
|
||||||
logger_ = rhs.logger_;
|
logger_ = rhs.logger_;
|
||||||
}
|
}
|
||||||
@ -8701,11 +8696,11 @@ inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) {
|
|||||||
inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert,
|
inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert,
|
||||||
std::size_t size) const {
|
std::size_t size) const {
|
||||||
auto mem = BIO_new_mem_buf(ca_cert, static_cast<int>(size));
|
auto mem = BIO_new_mem_buf(ca_cert, static_cast<int>(size));
|
||||||
|
auto se = detail::scope_exit([&] { BIO_free_all(mem); });
|
||||||
if (!mem) { return nullptr; }
|
if (!mem) { return nullptr; }
|
||||||
|
|
||||||
auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr);
|
auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr);
|
||||||
if (!inf) {
|
if (!inf) {
|
||||||
BIO_free_all(mem);
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -8721,7 +8716,6 @@ inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert,
|
|||||||
}
|
}
|
||||||
|
|
||||||
sk_X509_INFO_pop_free(inf, X509_INFO_free);
|
sk_X509_INFO_pop_free(inf, X509_INFO_free);
|
||||||
BIO_free_all(mem);
|
|
||||||
return cts;
|
return cts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user