@@ -3215,13 +3215,31 @@ inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) {
32153215
32163216template <bool Read>
32173217inline ssize_t select_impl (socket_t sock, time_t sec, time_t usec) {
3218+ #ifdef __APPLE__
3219+ if (sock >= FD_SETSIZE) { return -1 ; }
3220+
3221+ fd_set fds, *rfds, *wfds;
3222+ FD_ZERO (&fds);
3223+ FD_SET (sock, &fds);
3224+ rfds = (Read ? &fds : nullptr );
3225+ wfds = (Read ? nullptr : &fds);
3226+
3227+ timeval tv;
3228+ tv.tv_sec = static_cast <long >(sec);
3229+ tv.tv_usec = static_cast <decltype (tv.tv_usec )>(usec);
3230+
3231+ return handle_EINTR ([&]() {
3232+ return select (static_cast <int >(sock + 1 ), rfds, wfds, nullptr , &tv);
3233+ });
3234+ #else
32183235 struct pollfd pfd;
32193236 pfd.fd = sock;
32203237 pfd.events = (Read ? POLLIN : POLLOUT);
32213238
32223239 auto timeout = static_cast <int >(sec * 1000 + usec / 1000 );
32233240
32243241 return handle_EINTR ([&]() { return poll_wrapper (&pfd, 1 , timeout); });
3242+ #endif
32253243}
32263244
32273245inline ssize_t select_read (socket_t sock, time_t sec, time_t usec) {
@@ -3234,6 +3252,36 @@ inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) {
32343252
32353253inline Error wait_until_socket_is_ready (socket_t sock, time_t sec,
32363254 time_t usec) {
3255+ #ifdef __APPLE__
3256+ if (sock >= FD_SETSIZE) { return Error::Connection; }
3257+
3258+ fd_set fdsr, fdsw;
3259+ FD_ZERO (&fdsr);
3260+ FD_ZERO (&fdsw);
3261+ FD_SET (sock, &fdsr);
3262+ FD_SET (sock, &fdsw);
3263+
3264+ timeval tv;
3265+ tv.tv_sec = static_cast <long >(sec);
3266+ tv.tv_usec = static_cast <decltype (tv.tv_usec )>(usec);
3267+
3268+ auto ret = handle_EINTR ([&]() {
3269+ return select (static_cast <int >(sock + 1 ), &fdsr, &fdsw, nullptr , &tv);
3270+ });
3271+
3272+ if (ret == 0 ) { return Error::ConnectionTimeout; }
3273+
3274+ if (ret > 0 && (FD_ISSET (sock, &fdsr) || FD_ISSET (sock, &fdsw))) {
3275+ auto error = 0 ;
3276+ socklen_t len = sizeof (error);
3277+ auto res = getsockopt (sock, SOL_SOCKET, SO_ERROR,
3278+ reinterpret_cast <char *>(&error), &len);
3279+ auto successful = res >= 0 && !error;
3280+ return successful ? Error::Success : Error::Connection;
3281+ }
3282+
3283+ return Error::Connection;
3284+ #else
32373285 struct pollfd pfd_read;
32383286 pfd_read.fd = sock;
32393287 pfd_read.events = POLLIN | POLLOUT;
@@ -3255,6 +3303,7 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec,
32553303 }
32563304
32573305 return Error::Connection;
3306+ #endif
32583307}
32593308
32603309inline bool is_socket_alive (socket_t sock) {
@@ -8001,6 +8050,16 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
80018050 res.version = " HTTP/1.1" ;
80028051 res.headers = default_headers_;
80038052
8053+ #ifdef __APPLE__
8054+ // Socket file descriptor exceeded FD_SETSIZE...
8055+ if (strm.socket () >= FD_SETSIZE) {
8056+ Headers dummy;
8057+ detail::read_headers (strm, dummy);
8058+ res.status = StatusCode::InternalServerError_500;
8059+ return write_response (strm, close_connection, req, res);
8060+ }
8061+ #endif
8062+
80048063 // Request line and headers
80058064 if (!parse_request_line (line_reader.ptr (), req) ||
80068065 !detail::read_headers (strm, req.headers )) {
0 commit comments