Skip to content

Commit 644b591

Browse files
Remove select() and use poll()
1 parent e541dfc commit 644b591

File tree

3 files changed

+3
-82
lines changed

3 files changed

+3
-82
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM yhirose4dockerhub/ubuntu-builder AS builder
22
WORKDIR /build
33
COPY httplib.h .
44
COPY docker/main.cc .
5-
RUN g++ -std=c++23 -static -o server -O2 -I. -DCPPHTTPLIB_USE_POLL main.cc && strip server
5+
RUN g++ -std=c++23 -static -o server -O2 -I. main.cc && strip server
66

77
FROM scratch
88
COPY --from=builder /build/server /server

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -872,11 +872,6 @@ res->body; // Compressed data
872872

873873
```
874874

875-
Use `poll` instead of `select`
876-
------------------------------
877-
878-
`select` system call is used as default since it's more widely supported. If you want to let cpp-httplib use `poll` instead, you can do so with `CPPHTTPLIB_USE_POLL`.
879-
880875
Unix Domain Socket Support
881876
--------------------------
882877

httplib.h

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,7 @@ using ssize_t = long;
194194

195195
using socket_t = SOCKET;
196196
using socklen_t = int;
197-
#ifdef CPPHTTPLIB_USE_POLL
198197
#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
199-
#endif
200198

201199
#else // not _WIN32
202200

@@ -216,16 +214,11 @@ using socklen_t = int;
216214
#ifdef __linux__
217215
#include <resolv.h>
218216
#endif
217+
#include <csignal>
219218
#include <netinet/tcp.h>
220-
#ifdef CPPHTTPLIB_USE_POLL
221219
#include <poll.h>
222-
#endif
223-
#include <csignal>
224220
#include <pthread.h>
225221
#include <sys/mman.h>
226-
#ifndef __VMS
227-
#include <sys/select.h>
228-
#endif
229222
#include <sys/socket.h>
230223
#include <sys/un.h>
231224
#include <unistd.h>
@@ -3259,33 +3252,13 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size,
32593252

32603253
template <bool Read>
32613254
inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) {
3262-
#ifdef CPPHTTPLIB_USE_POLL
32633255
struct pollfd pfd;
32643256
pfd.fd = sock;
32653257
pfd.events = (Read ? POLLIN : POLLOUT);
32663258

32673259
auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
32683260

32693261
return handle_EINTR([&]() { return poll(&pfd, 1, timeout); });
3270-
#else
3271-
#ifndef _WIN32
3272-
if (sock >= FD_SETSIZE) { return -1; }
3273-
#endif
3274-
3275-
fd_set fds, *rfds, *wfds;
3276-
FD_ZERO(&fds);
3277-
FD_SET(sock, &fds);
3278-
rfds = (Read ? &fds : nullptr);
3279-
wfds = (Read ? nullptr : &fds);
3280-
3281-
timeval tv;
3282-
tv.tv_sec = static_cast<long>(sec);
3283-
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);
3284-
3285-
return handle_EINTR([&]() {
3286-
return select(static_cast<int>(sock + 1), rfds, wfds, nullptr, &tv);
3287-
});
3288-
#endif
32893262
}
32903263

32913264
inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) {
@@ -3298,7 +3271,6 @@ inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) {
32983271

32993272
inline Error wait_until_socket_is_ready(socket_t sock, time_t sec,
33003273
time_t usec) {
3301-
#ifdef CPPHTTPLIB_USE_POLL
33023274
struct pollfd pfd_read;
33033275
pfd_read.fd = sock;
33043276
pfd_read.events = POLLIN | POLLOUT;
@@ -3319,38 +3291,6 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec,
33193291
}
33203292

33213293
return Error::Connection;
3322-
#else
3323-
#ifndef _WIN32
3324-
if (sock >= FD_SETSIZE) { return Error::Connection; }
3325-
#endif
3326-
3327-
fd_set fdsr;
3328-
FD_ZERO(&fdsr);
3329-
FD_SET(sock, &fdsr);
3330-
3331-
auto fdsw = fdsr;
3332-
auto fdse = fdsr;
3333-
3334-
timeval tv;
3335-
tv.tv_sec = static_cast<long>(sec);
3336-
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);
3337-
3338-
auto ret = handle_EINTR([&]() {
3339-
return select(static_cast<int>(sock + 1), &fdsr, &fdsw, &fdse, &tv);
3340-
});
3341-
3342-
if (ret == 0) { return Error::ConnectionTimeout; }
3343-
3344-
if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) {
3345-
auto error = 0;
3346-
socklen_t len = sizeof(error);
3347-
auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR,
3348-
reinterpret_cast<char *>(&error), &len);
3349-
auto successful = res >= 0 && !error;
3350-
return successful ? Error::Success : Error::Connection;
3351-
}
3352-
return Error::Connection;
3353-
#endif
33543294
}
33553295

33563296
inline bool is_socket_alive(socket_t sock) {
@@ -7196,20 +7136,6 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
71967136
res.version = "HTTP/1.1";
71977137
res.headers = default_headers_;
71987138

7199-
#ifdef _WIN32
7200-
// TODO: Increase FD_SETSIZE statically (libzmq), dynamically (MySQL).
7201-
#else
7202-
#ifndef CPPHTTPLIB_USE_POLL
7203-
// Socket file descriptor exceeded FD_SETSIZE...
7204-
if (strm.socket() >= FD_SETSIZE) {
7205-
Headers dummy;
7206-
detail::read_headers(strm, dummy);
7207-
res.status = StatusCode::InternalServerError_500;
7208-
return write_response(strm, close_connection, req, res);
7209-
}
7210-
#endif
7211-
#endif
7212-
72137139
// Request line and headers
72147140
if (!parse_request_line(line_reader.ptr(), req) ||
72157141
!detail::read_headers(strm, req.headers)) {
@@ -10440,7 +10366,7 @@ inline SSL_CTX *Client::ssl_context() const {
1044010366

1044110367
} // namespace httplib
1044210368

10443-
#if defined(_WIN32) && defined(CPPHTTPLIB_USE_POLL)
10369+
#ifdef _WIN32
1044410370
#undef poll
1044510371
#endif
1044610372

0 commit comments

Comments
 (0)