1
0
mirror of synced 2025-04-21 22:25:55 +03:00

Code cleanup

This commit is contained in:
yhirose 2021-12-31 13:27:47 -05:00
parent f817032513
commit 070f9bec58

View File

@ -217,7 +217,8 @@ using socket_t = int;
#include <thread> #include <thread>
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
// these are defined in wincrypt.h and it breaks compilation if BoringSSL is used // these are defined in wincrypt.h and it breaks compilation if BoringSSL is
// used
#ifdef _WIN32 #ifdef _WIN32
#undef X509_NAME #undef X509_NAME
#undef X509_CERT_PAIR #undef X509_CERT_PAIR
@ -1951,7 +1952,7 @@ inline std::string base64_encode(const std::string &in) {
inline bool is_file(const std::string &path) { inline bool is_file(const std::string &path) {
#ifdef _WIN32 #ifdef _WIN32
return (_access_s(path.c_str(), 0 ) == 0); return _access_s(path.c_str(), 0) == 0;
#else #else
struct stat st; struct stat st;
return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode); return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode);
@ -2688,25 +2689,29 @@ inline socket_t create_client_socket(
{ {
#ifdef _WIN32 #ifdef _WIN32
uint32_t timeout = static_cast<uint32_t>(read_timeout_sec * 1000 + read_timeout_usec / 1000); auto timeout = static_cast<uint32_t>(read_timeout_sec * 1000 +
setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)); read_timeout_usec / 1000);
setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout));
#else #else
timeval tv; timeval tv;
tv.tv_sec = static_cast<long>(read_timeout_sec); tv.tv_sec = static_cast<long>(read_timeout_sec);
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(read_timeout_usec); tv.tv_usec = static_cast<decltype(tv.tv_usec)>(read_timeout_usec);
setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
#endif #endif
} }
{ {
#ifdef _WIN32 #ifdef _WIN32
uint32_t timeout = static_cast<uint32_t>(write_timeout_sec * 1000 + write_timeout_usec / 1000); auto timeout = static_cast<uint32_t>(write_timeout_sec * 1000 +
setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)); write_timeout_usec / 1000);
setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
sizeof(timeout));
#else #else
timeval tv; timeval tv;
tv.tv_sec = static_cast<long>(read_timeout_sec); tv.tv_sec = static_cast<long>(read_timeout_sec);
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(read_timeout_usec); tv.tv_usec = static_cast<decltype(tv.tv_usec)>(read_timeout_usec);
setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv));
#endif #endif
} }
@ -5254,29 +5259,33 @@ inline bool Server::listen_internal() {
break; break;
} }
{ {
#ifdef _WIN32 #ifdef _WIN32
uint32_t timeout = static_cast<uint32_t>(read_timeout_sec_ * 1000 + read_timeout_usec_ / 1000); auto timeout = static_cast<uint32_t>(read_timeout_sec_ * 1000 +
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)); read_timeout_usec_ / 1000);
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout));
#else #else
timeval tv; timeval tv;
tv.tv_sec = static_cast<long>(read_timeout_sec_); tv.tv_sec = static_cast<long>(read_timeout_sec_);
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(read_timeout_usec_); tv.tv_usec = static_cast<decltype(tv.tv_usec)>(read_timeout_usec_);
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
#endif #endif
} }
{ {
#ifdef _WIN32 #ifdef _WIN32
uint32_t timeout = static_cast<uint32_t>(write_timeout_sec_ * 1000 + write_timeout_usec_ / 1000); auto timeout = static_cast<uint32_t>(write_timeout_sec_ * 1000 +
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)); write_timeout_usec_ / 1000);
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
sizeof(timeout));
#else #else
timeval tv; timeval tv;
tv.tv_sec = static_cast<long>(read_timeout_sec_); tv.tv_sec = static_cast<long>(read_timeout_sec_);
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(read_timeout_usec_); tv.tv_usec = static_cast<decltype(tv.tv_usec)>(read_timeout_usec_);
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv));
#endif #endif
} }
#if __cplusplus > 201703L #if __cplusplus > 201703L
task_queue->enqueue([=, this]() { process_and_close_socket(sock); }); task_queue->enqueue([=, this]() { process_and_close_socket(sock); });