145145#define CPPHTTPLIB_LISTEN_BACKLOG 5
146146#endif
147147
148- #if !defined(CPPHTTPLIB_USE_POLL) && !defined(CPPHTTPLIB_USE_SELECT)
149- #define CPPHTTPLIB_USE_POLL
150- #elif defined(CPPHTTPLIB_USE_POLL) && defined(CPPHTTPLIB_USE_SELECT)
151- #error "CPPHTTPLIB_USE_POLL and CPPHTTPLIB_USE_SELECT are mutually exclusive"
152- #endif
153-
154148/*
155149 * Headers
156150 */
@@ -200,9 +194,7 @@ using ssize_t = long;
200194
201195using socket_t = SOCKET;
202196using socklen_t = int ;
203- #ifdef CPPHTTPLIB_USE_POLL
204197#define poll (fds, nfds, timeout ) WSAPoll(fds, nfds, timeout)
205- #endif
206198
207199#else // not _WIN32
208200
@@ -222,16 +214,11 @@ using socklen_t = int;
222214#ifdef __linux__
223215#include < resolv.h>
224216#endif
217+ #include < csignal>
225218#include < netinet/tcp.h>
226- #ifdef CPPHTTPLIB_USE_POLL
227219#include < poll.h>
228- #endif
229- #include < csignal>
230220#include < pthread.h>
231221#include < sys/mman.h>
232- #ifndef __VMS
233- #include < sys/select.h>
234- #endif
235222#include < sys/socket.h>
236223#include < sys/un.h>
237224#include < unistd.h>
@@ -3267,33 +3254,13 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size,
32673254
32683255template <bool Read>
32693256inline ssize_t select_impl (socket_t sock, time_t sec, time_t usec) {
3270- #ifdef CPPHTTPLIB_USE_POLL
32713257 struct pollfd pfd;
32723258 pfd.fd = sock;
32733259 pfd.events = (Read ? POLLIN : POLLOUT);
32743260
32753261 auto timeout = static_cast <int >(sec * 1000 + usec / 1000 );
32763262
32773263 return handle_EINTR ([&]() { return poll (&pfd, 1 , timeout); });
3278- #else
3279- #ifndef _WIN32
3280- if (sock >= FD_SETSIZE) { return -1 ; }
3281- #endif
3282-
3283- fd_set fds, *rfds, *wfds;
3284- FD_ZERO (&fds);
3285- FD_SET (sock, &fds);
3286- rfds = (Read ? &fds : nullptr );
3287- wfds = (Read ? nullptr : &fds);
3288-
3289- timeval tv;
3290- tv.tv_sec = static_cast <long >(sec);
3291- tv.tv_usec = static_cast <decltype (tv.tv_usec )>(usec);
3292-
3293- return handle_EINTR ([&]() {
3294- return select (static_cast <int >(sock + 1 ), rfds, wfds, nullptr , &tv);
3295- });
3296- #endif
32973264}
32983265
32993266inline ssize_t select_read (socket_t sock, time_t sec, time_t usec) {
@@ -3306,7 +3273,6 @@ inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) {
33063273
33073274inline Error wait_until_socket_is_ready (socket_t sock, time_t sec,
33083275 time_t usec) {
3309- #ifdef CPPHTTPLIB_USE_POLL
33103276 struct pollfd pfd_read;
33113277 pfd_read.fd = sock;
33123278 pfd_read.events = POLLIN | POLLOUT;
@@ -3327,38 +3293,6 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec,
33273293 }
33283294
33293295 return Error::Connection;
3330- #else
3331- #ifndef _WIN32
3332- if (sock >= FD_SETSIZE) { return Error::Connection; }
3333- #endif
3334-
3335- fd_set fdsr;
3336- FD_ZERO (&fdsr);
3337- FD_SET (sock, &fdsr);
3338-
3339- auto fdsw = fdsr;
3340- auto fdse = fdsr;
3341-
3342- timeval tv;
3343- tv.tv_sec = static_cast <long >(sec);
3344- tv.tv_usec = static_cast <decltype (tv.tv_usec )>(usec);
3345-
3346- auto ret = handle_EINTR ([&]() {
3347- return select (static_cast <int >(sock + 1 ), &fdsr, &fdsw, &fdse, &tv);
3348- });
3349-
3350- if (ret == 0 ) { return Error::ConnectionTimeout; }
3351-
3352- if (ret > 0 && (FD_ISSET (sock, &fdsr) || FD_ISSET (sock, &fdsw))) {
3353- auto error = 0 ;
3354- socklen_t len = sizeof (error);
3355- auto res = getsockopt (sock, SOL_SOCKET, SO_ERROR,
3356- reinterpret_cast <char *>(&error), &len);
3357- auto successful = res >= 0 && !error;
3358- return successful ? Error::Success : Error::Connection;
3359- }
3360- return Error::Connection;
3361- #endif
33623296}
33633297
33643298inline bool is_socket_alive (socket_t sock) {
@@ -7208,20 +7142,6 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
72087142 res.version = " HTTP/1.1" ;
72097143 res.headers = default_headers_;
72107144
7211- #ifdef _WIN32
7212- // TODO: Increase FD_SETSIZE statically (libzmq), dynamically (MySQL).
7213- #else
7214- #ifndef CPPHTTPLIB_USE_POLL
7215- // Socket file descriptor exceeded FD_SETSIZE...
7216- if (strm.socket () >= FD_SETSIZE) {
7217- Headers dummy;
7218- detail::read_headers (strm, dummy);
7219- res.status = StatusCode::InternalServerError_500;
7220- return write_response (strm, close_connection, req, res);
7221- }
7222- #endif
7223- #endif
7224-
72257145 // Request line and headers
72267146 if (!parse_request_line (line_reader.ptr (), req) ||
72277147 !detail::read_headers (strm, req.headers )) {
@@ -10456,7 +10376,7 @@ inline SSL_CTX *Client::ssl_context() const {
1045610376
1045710377} // namespace httplib
1045810378
10459- #if defined( _WIN32) && defined(CPPHTTPLIB_USE_POLL)
10379+ #ifdef _WIN32
1046010380#undef poll
1046110381#endif
1046210382
0 commit comments