1
0
mirror of synced 2025-09-05 13:43:59 +03:00

Fix #2193. Allow _WIN32

This commit is contained in:
yhirose
2025-08-06 23:05:42 -04:00
parent 70cca55caf
commit fbee136dca

View File

@@ -16,7 +16,7 @@
*/ */
#if defined(_WIN32) && !defined(_WIN64) #if defined(_WIN32) && !defined(_WIN64)
#error \ #warning \
"cpp-httplib doesn't support 32-bit Windows. Please use a 64-bit compiler." "cpp-httplib doesn't support 32-bit Windows. Please use a 64-bit compiler."
#elif defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ < 8 #elif defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ < 8
#warning \ #warning \
@@ -98,7 +98,7 @@
#endif #endif
#ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND #ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND
#ifdef _WIN64 #ifdef _WIN32
#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 1000 #define CPPHTTPLIB_IDLE_INTERVAL_USECOND 1000
#else #else
#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0 #define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0
@@ -184,7 +184,7 @@
* Headers * Headers
*/ */
#ifdef _WIN64 #ifdef _WIN32
#ifndef _CRT_SECURE_NO_WARNINGS #ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#endif //_CRT_SECURE_NO_WARNINGS #endif //_CRT_SECURE_NO_WARNINGS
@@ -235,7 +235,7 @@ using nfds_t = unsigned long;
using socket_t = SOCKET; using socket_t = SOCKET;
using socklen_t = int; using socklen_t = int;
#else // not _WIN64 #else // not _WIN32
#include <arpa/inet.h> #include <arpa/inet.h>
#if !defined(_AIX) && !defined(__MVS__) #if !defined(_AIX) && !defined(__MVS__)
@@ -266,7 +266,7 @@ using socket_t = int;
#ifndef INVALID_SOCKET #ifndef INVALID_SOCKET
#define INVALID_SOCKET (-1) #define INVALID_SOCKET (-1)
#endif #endif
#endif //_WIN64 #endif //_WIN32
#if defined(__APPLE__) #if defined(__APPLE__)
#include <TargetConditionals.h> #include <TargetConditionals.h>
@@ -311,7 +311,7 @@ using socket_t = int;
// CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN // CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
#ifdef _WIN64 #ifdef _WIN32
#include <wincrypt.h> #include <wincrypt.h>
// these are defined in wincrypt.h and it breaks compilation if BoringSSL is // these are defined in wincrypt.h and it breaks compilation if BoringSSL is
@@ -324,7 +324,7 @@ using socket_t = int;
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma comment(lib, "crypt32.lib") #pragma comment(lib, "crypt32.lib")
#endif #endif
#endif // _WIN64 #endif // _WIN32
#if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) #if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
#if TARGET_OS_OSX #if TARGET_OS_OSX
@@ -337,7 +337,7 @@ using socket_t = int;
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <openssl/x509v3.h> #include <openssl/x509v3.h>
#if defined(_WIN64) && defined(OPENSSL_USE_APPLINK) #if defined(_WIN32) && defined(OPENSSL_USE_APPLINK)
#include <openssl/applink.c> #include <openssl/applink.c>
#endif #endif
@@ -2086,7 +2086,7 @@ namespace detail {
inline bool set_socket_opt_impl(socket_t sock, int level, int optname, inline bool set_socket_opt_impl(socket_t sock, int level, int optname,
const void *optval, socklen_t optlen) { const void *optval, socklen_t optlen) {
return setsockopt(sock, level, optname, return setsockopt(sock, level, optname,
#ifdef _WIN64 #ifdef _WIN32
reinterpret_cast<const char *>(optval), reinterpret_cast<const char *>(optval),
#else #else
optval, optval,
@@ -2100,7 +2100,7 @@ inline bool set_socket_opt(socket_t sock, int level, int optname, int optval) {
inline bool set_socket_opt_time(socket_t sock, int level, int optname, inline bool set_socket_opt_time(socket_t sock, int level, int optname,
time_t sec, time_t usec) { time_t sec, time_t usec) {
#ifdef _WIN64 #ifdef _WIN32
auto timeout = static_cast<uint32_t>(sec * 1000 + usec / 1000); auto timeout = static_cast<uint32_t>(sec * 1000 + usec / 1000);
#else #else
timeval timeout; timeval timeout;
@@ -2378,7 +2378,7 @@ make_basic_authentication_header(const std::string &username,
namespace detail { namespace detail {
#if defined(_WIN64) #if defined(_WIN32)
inline std::wstring u8string_to_wstring(const char *s) { inline std::wstring u8string_to_wstring(const char *s) {
std::wstring ws; std::wstring ws;
auto len = static_cast<int>(strlen(s)); auto len = static_cast<int>(strlen(s));
@@ -2400,7 +2400,7 @@ struct FileStat {
bool is_dir() const; bool is_dir() const;
private: private:
#if defined(_WIN64) #if defined(_WIN32)
struct _stat st_; struct _stat st_;
#else #else
struct stat st_; struct stat st_;
@@ -2641,7 +2641,7 @@ public:
const char *data() const; const char *data() const;
private: private:
#if defined(_WIN64) #if defined(_WIN32)
HANDLE hFile_ = NULL; HANDLE hFile_ = NULL;
HANDLE hMapping_ = NULL; HANDLE hMapping_ = NULL;
#else #else
@@ -2862,7 +2862,7 @@ inline bool is_valid_path(const std::string &path) {
} }
inline FileStat::FileStat(const std::string &path) { inline FileStat::FileStat(const std::string &path) {
#if defined(_WIN64) #if defined(_WIN32)
auto wpath = u8string_to_wstring(path.c_str()); auto wpath = u8string_to_wstring(path.c_str());
ret_ = _wstat(wpath.c_str(), &st_); ret_ = _wstat(wpath.c_str(), &st_);
#else #else
@@ -3074,7 +3074,7 @@ inline mmap::~mmap() { close(); }
inline bool mmap::open(const char *path) { inline bool mmap::open(const char *path) {
close(); close();
#if defined(_WIN64) #if defined(_WIN32)
auto wpath = u8string_to_wstring(path); auto wpath = u8string_to_wstring(path);
if (wpath.empty()) { return false; } if (wpath.empty()) { return false; }
@@ -3151,7 +3151,7 @@ inline const char *mmap::data() const {
} }
inline void mmap::close() { inline void mmap::close() {
#if defined(_WIN64) #if defined(_WIN32)
if (addr_) { if (addr_) {
::UnmapViewOfFile(addr_); ::UnmapViewOfFile(addr_);
addr_ = nullptr; addr_ = nullptr;
@@ -3182,7 +3182,7 @@ inline void mmap::close() {
size_ = 0; size_ = 0;
} }
inline int close_socket(socket_t sock) { inline int close_socket(socket_t sock) {
#ifdef _WIN64 #ifdef _WIN32
return closesocket(sock); return closesocket(sock);
#else #else
return close(sock); return close(sock);
@@ -3205,7 +3205,7 @@ template <typename T> inline ssize_t handle_EINTR(T fn) {
inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) {
return handle_EINTR([&]() { return handle_EINTR([&]() {
return recv(sock, return recv(sock,
#ifdef _WIN64 #ifdef _WIN32
static_cast<char *>(ptr), static_cast<int>(size), static_cast<char *>(ptr), static_cast<int>(size),
#else #else
ptr, size, ptr, size,
@@ -3218,7 +3218,7 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size,
int flags) { int flags) {
return handle_EINTR([&]() { return handle_EINTR([&]() {
return send(sock, return send(sock,
#ifdef _WIN64 #ifdef _WIN32
static_cast<const char *>(ptr), static_cast<int>(size), static_cast<const char *>(ptr), static_cast<int>(size),
#else #else
ptr, size, ptr, size,
@@ -3228,7 +3228,7 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size,
} }
inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) { inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) {
#ifdef _WIN64 #ifdef _WIN32
return ::WSAPoll(fds, nfds, timeout); return ::WSAPoll(fds, nfds, timeout);
#else #else
return ::poll(fds, nfds, timeout); return ::poll(fds, nfds, timeout);
@@ -3487,7 +3487,7 @@ inline bool process_client_socket(
} }
inline int shutdown_socket(socket_t sock) { inline int shutdown_socket(socket_t sock) {
#ifdef _WIN64 #ifdef _WIN32
return shutdown(sock, SD_BOTH); return shutdown(sock, SD_BOTH);
#else #else
return shutdown(sock, SHUT_RDWR); return shutdown(sock, SHUT_RDWR);
@@ -3522,7 +3522,7 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service,
return getaddrinfo(node, service, hints, res); return getaddrinfo(node, service, hints, res);
} }
#ifdef _WIN64 #ifdef _WIN32
// Windows-specific implementation using GetAddrInfoEx with overlapped I/O // Windows-specific implementation using GetAddrInfoEx with overlapped I/O
OVERLAPPED overlapped = {0}; OVERLAPPED overlapped = {0};
HANDLE event = CreateEventW(nullptr, TRUE, FALSE, nullptr); HANDLE event = CreateEventW(nullptr, TRUE, FALSE, nullptr);
@@ -3855,7 +3855,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
hints.ai_flags = socket_flags; hints.ai_flags = socket_flags;
} }
#if !defined(_WIN64) || defined(CPPHTTPLIB_HAVE_AFUNIX_H) #if !defined(_WIN32) || defined(CPPHTTPLIB_HAVE_AFUNIX_H)
if (hints.ai_family == AF_UNIX) { if (hints.ai_family == AF_UNIX) {
const auto addrlen = host.length(); const auto addrlen = host.length();
if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; }
@@ -3879,14 +3879,14 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
sizeof(addr) - sizeof(addr.sun_path) + addrlen); sizeof(addr) - sizeof(addr.sun_path) + addrlen);
#ifndef SOCK_CLOEXEC #ifndef SOCK_CLOEXEC
#ifndef _WIN64 #ifndef _WIN32
fcntl(sock, F_SETFD, FD_CLOEXEC); fcntl(sock, F_SETFD, FD_CLOEXEC);
#endif #endif
#endif #endif
if (socket_options) { socket_options(sock); } if (socket_options) { socket_options(sock); }
#ifdef _WIN64 #ifdef _WIN32
// Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so // Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so
// remove the option. // remove the option.
detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0); detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0);
@@ -3915,7 +3915,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
for (auto rp = result; rp; rp = rp->ai_next) { for (auto rp = result; rp; rp = rp->ai_next) {
// Create a socket // Create a socket
#ifdef _WIN64 #ifdef _WIN32
auto sock = auto sock =
WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0,
WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED); WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED);
@@ -3948,7 +3948,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
#endif #endif
if (sock == INVALID_SOCKET) { continue; } if (sock == INVALID_SOCKET) { continue; }
#if !defined _WIN64 && !defined SOCK_CLOEXEC #if !defined _WIN32 && !defined SOCK_CLOEXEC
if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) {
close_socket(sock); close_socket(sock);
continue; continue;
@@ -3976,7 +3976,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
} }
inline void set_nonblocking(socket_t sock, bool nonblocking) { inline void set_nonblocking(socket_t sock, bool nonblocking) {
#ifdef _WIN64 #ifdef _WIN32
auto flags = nonblocking ? 1UL : 0UL; auto flags = nonblocking ? 1UL : 0UL;
ioctlsocket(sock, FIONBIO, &flags); ioctlsocket(sock, FIONBIO, &flags);
#else #else
@@ -3987,7 +3987,7 @@ inline void set_nonblocking(socket_t sock, bool nonblocking) {
} }
inline bool is_connection_error() { inline bool is_connection_error() {
#ifdef _WIN64 #ifdef _WIN32
return WSAGetLastError() != WSAEWOULDBLOCK; return WSAGetLastError() != WSAEWOULDBLOCK;
#else #else
return errno != EINPROGRESS; return errno != EINPROGRESS;
@@ -4021,7 +4021,7 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) {
return ret; return ret;
} }
#if !defined _WIN64 && !defined ANDROID && !defined _AIX && !defined __MVS__ #if !defined _WIN32 && !defined ANDROID && !defined _AIX && !defined __MVS__
#define USE_IF2IP #define USE_IF2IP
#endif #endif
@@ -4160,7 +4160,7 @@ inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) {
if (!getpeername(sock, reinterpret_cast<struct sockaddr *>(&addr), if (!getpeername(sock, reinterpret_cast<struct sockaddr *>(&addr),
&addr_len)) { &addr_len)) {
#ifndef _WIN64 #ifndef _WIN32
if (addr.ss_family == AF_UNIX) { if (addr.ss_family == AF_UNIX) {
#if defined(__linux__) #if defined(__linux__)
struct ucred ucred; struct ucred ucred;
@@ -6226,7 +6226,7 @@ inline bool is_ssl_peer_could_be_closed(SSL *ssl, socket_t sock) {
SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN;
} }
#ifdef _WIN64 #ifdef _WIN32
// NOTE: This code came up with the following stackoverflow post: // NOTE: This code came up with the following stackoverflow post:
// https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store // https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store
inline bool load_system_certs_on_windows(X509_STORE *store) { inline bool load_system_certs_on_windows(X509_STORE *store) {
@@ -6342,10 +6342,10 @@ inline bool load_system_certs_on_macos(X509_STORE *store) {
return result; return result;
} }
#endif // _WIN64 #endif // _WIN32
#endif // CPPHTTPLIB_OPENSSL_SUPPORT #endif // CPPHTTPLIB_OPENSSL_SUPPORT
#ifdef _WIN64 #ifdef _WIN32
class WSInit { class WSInit {
public: public:
WSInit() { WSInit() {
@@ -7041,7 +7041,7 @@ inline bool SocketStream::wait_writable() const {
} }
inline ssize_t SocketStream::read(char *ptr, size_t size) { inline ssize_t SocketStream::read(char *ptr, size_t size) {
#ifdef _WIN64 #ifdef _WIN32
size = size =
(std::min)(size, static_cast<size_t>((std::numeric_limits<int>::max)())); (std::min)(size, static_cast<size_t>((std::numeric_limits<int>::max)()));
#else #else
@@ -7089,7 +7089,7 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) {
inline ssize_t SocketStream::write(const char *ptr, size_t size) { inline ssize_t SocketStream::write(const char *ptr, size_t size) {
if (!wait_writable()) { return -1; } if (!wait_writable()) { return -1; }
#if defined(_WIN64) && !defined(_WIN64) #if defined(_WIN32) && !defined(_WIN64)
size = size =
(std::min)(size, static_cast<size_t>((std::numeric_limits<int>::max)())); (std::min)(size, static_cast<size_t>((std::numeric_limits<int>::max)()));
#endif #endif
@@ -7252,7 +7252,7 @@ inline bool RegexMatcher::match(Request &request) const {
inline Server::Server() inline Server::Server()
: new_task_queue( : new_task_queue(
[] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) { [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) {
#ifndef _WIN64 #ifndef _WIN32
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
#endif #endif
} }
@@ -7950,7 +7950,7 @@ inline bool Server::listen_internal() {
std::unique_ptr<TaskQueue> task_queue(new_task_queue()); std::unique_ptr<TaskQueue> task_queue(new_task_queue());
while (svr_sock_ != INVALID_SOCKET) { while (svr_sock_ != INVALID_SOCKET) {
#ifndef _WIN64 #ifndef _WIN32
if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) { if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) {
#endif #endif
auto val = detail::select_read(svr_sock_, idle_interval_sec_, auto val = detail::select_read(svr_sock_, idle_interval_sec_,
@@ -7959,11 +7959,11 @@ inline bool Server::listen_internal() {
task_queue->on_idle(); task_queue->on_idle();
continue; continue;
} }
#ifndef _WIN64 #ifndef _WIN32
} }
#endif #endif
#if defined _WIN64 #if defined _WIN32
// sockets connected via WASAccept inherit flags NO_HANDLE_INHERIT, // sockets connected via WASAccept inherit flags NO_HANDLE_INHERIT,
// OVERLAPPED // OVERLAPPED
socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0);
@@ -10571,7 +10571,7 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) {
if (ret < 0) { if (ret < 0) {
auto err = SSL_get_error(ssl_, ret); auto err = SSL_get_error(ssl_, ret);
auto n = 1000; auto n = 1000;
#ifdef _WIN64 #ifdef _WIN32
while (--n >= 0 && (err == SSL_ERROR_WANT_READ || while (--n >= 0 && (err == SSL_ERROR_WANT_READ ||
(err == SSL_ERROR_SYSCALL && (err == SSL_ERROR_SYSCALL &&
WSAGetLastError() == WSAETIMEDOUT))) { WSAGetLastError() == WSAETIMEDOUT))) {
@@ -10606,7 +10606,7 @@ inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) {
if (ret < 0) { if (ret < 0) {
auto err = SSL_get_error(ssl_, ret); auto err = SSL_get_error(ssl_, ret);
auto n = 1000; auto n = 1000;
#ifdef _WIN64 #ifdef _WIN32
while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE || while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE ||
(err == SSL_ERROR_SYSCALL && (err == SSL_ERROR_SYSCALL &&
WSAGetLastError() == WSAETIMEDOUT))) { WSAGetLastError() == WSAETIMEDOUT))) {
@@ -11000,13 +11000,13 @@ inline bool SSLClient::load_certs() {
} }
} else { } else {
auto loaded = false; auto loaded = false;
#ifdef _WIN64 #ifdef _WIN32
loaded = loaded =
detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_));
#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && \ #elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && \
defined(TARGET_OS_OSX) defined(TARGET_OS_OSX)
loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_));
#endif // _WIN64 #endif // _WIN32
if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); }
} }
}); });