Skip to content

Commit 22d90c2

Browse files
Remove select() and use poll() (#2078)
* Revert "Fix typo in meson.build (#2070)" This reverts commit 5c0135f. * Revert "build(meson): automatically use poll or select as needed (#2067)" This reverts commit 2b5d1ee. * Revert "Make poll() the default (#2065)" This reverts commit 6e73a63. * Remove select() and use poll()
1 parent b944f94 commit 22d90c2

File tree

6 files changed

+7
-144
lines changed

6 files changed

+7
-144
lines changed

.github/workflows/test.yaml

+4-38
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,14 @@ jobs:
4343
(github.event_name == 'pull_request' &&
4444
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
4545
(github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true')
46-
strategy:
47-
matrix:
48-
select_impl: ['select', 'poll']
4946
steps:
5047
- name: checkout
5148
uses: actions/checkout@v4
5249
- name: install libraries
5350
run: sudo apt-get update && sudo apt-get install -y libbrotli-dev libcurl4-openssl-dev
5451
- name: build and run tests
55-
env:
56-
SELECT_IMPL: ${{ matrix.select_impl }}
5752
run: cd test && make
5853
- name: run fuzz test target
59-
env:
60-
SELECT_IMPL: ${{ matrix.select_impl }}
6154
run: cd test && make fuzz_test
6255

6356
macos:
@@ -67,19 +60,12 @@ jobs:
6760
(github.event_name == 'pull_request' &&
6861
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
6962
(github.event_name == 'workflow_dispatch' && github.event.inputs.test_macos == 'true')
70-
strategy:
71-
matrix:
72-
select_impl: ['select', 'poll']
7363
steps:
7464
- name: checkout
7565
uses: actions/checkout@v4
7666
- name: build and run tests
77-
env:
78-
SELECT_IMPL: ${{ matrix.select_impl }}
7967
run: cd test && make
8068
- name: run fuzz test target
81-
env:
82-
SELECT_IMPL: ${{ matrix.select_impl }}
8369
run: cd test && make fuzz_test
8470

8571
windows:
@@ -89,9 +75,6 @@ jobs:
8975
(github.event_name == 'pull_request' &&
9076
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
9177
(github.event_name == 'workflow_dispatch' && github.event.inputs.test_windows == 'true')
92-
strategy:
93-
matrix:
94-
select_impl: ['select', 'poll']
9578
steps:
9679
- name: Prepare Git for Checkout on Windows
9780
run: |
@@ -113,33 +96,16 @@ jobs:
11396
choco install openssl
11497
11598
- name: Configure CMake with SSL
116-
run: >
117-
cmake -B build -S .
118-
-DCMAKE_BUILD_TYPE=Release
119-
-DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake
120-
-DHTTPLIB_TEST=ON
121-
-DHTTPLIB_REQUIRE_OPENSSL=ON
122-
-DHTTPLIB_REQUIRE_ZLIB=ON
123-
-DHTTPLIB_REQUIRE_BROTLI=ON
124-
-DHTTPLIB_USE_SELECT=${{ matrix.select_impl == 'select' && 'ON' || 'OFF' }}
99+
run: cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=ON -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON
125100
- name: Build with with SSL
126-
run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine /nologo
101+
run: cmake --build build --config Release
127102
- name: Run tests with SSL
128103
run: ctest --output-on-failure --test-dir build -C Release
129104

130105
- name: Configure CMake without SSL
131-
run: >
132-
cmake -B build-no-ssl -S .
133-
-DCMAKE_BUILD_TYPE=Release
134-
-DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake
135-
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
136-
-DHTTPLIB_TEST=ON
137-
-DHTTPLIB_REQUIRE_OPENSSL=OFF
138-
-DHTTPLIB_REQUIRE_ZLIB=ON
139-
-DHTTPLIB_REQUIRE_BROTLI=ON
140-
-DHTTPLIB_USE_SELECT=${{ matrix.select_impl == 'select' && 'ON' || 'OFF' }}
106+
run: cmake -B build-no-ssl -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=OFF -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON
141107
- name: Build without SSL
142-
run: cmake --build build-no-ssl --config Release -- /v:m /clp:ShowCommandLine /nologo
108+
run: cmake --build build-no-ssl --config Release
143109
- name: Run tests without SSL
144110
run: ctest --output-on-failure --test-dir build-no-ssl -C Release
145111
env:

CMakeLists.txt

-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* HTTPLIB_REQUIRE_ZLIB (default off)
99
* HTTPLIB_REQUIRE_BROTLI (default off)
1010
* HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on)
11-
* HTTPLIB_USE_SELECT (default off) choose between select() and poll()
1211
* HTTPLIB_COMPILE (default off)
1312
* HTTPLIB_INSTALL (default on)
1413
* HTTPLIB_TEST (default off)
@@ -47,7 +46,6 @@
4746
* HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled.
4847
* HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled.
4948
* HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN - a bool for if support of loading system certs from the Apple Keychain is enabled.
50-
* HTTPLIB_IS_USING_SELECT - a bool for if select() is used instead of poll().
5149
* HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only.
5250
* HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include).
5351
* HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so).
@@ -103,7 +101,6 @@ option(HTTPLIB_TEST "Enables testing and builds tests" OFF)
103101
option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF)
104102
option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON)
105103
option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system certs from the Apple Keychain." ON)
106-
option(HTTPLIB_USE_SELECT "Uses select() instead of poll()." OFF)
107104
# Defaults to static library
108105
option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF)
109106
if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE)
@@ -115,7 +112,6 @@ endif()
115112
# Set some variables that are used in-tree and while building based on our options
116113
set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE})
117114
set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN ${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN})
118-
set(HTTPLIB_IS_USING_SELECT ${HTTPLIB_USE_SELECT})
119115

120116
# Threads needed for <thread> on some systems, and for <pthread.h> on Linux
121117
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
@@ -242,7 +238,6 @@ target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
242238
$<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:CPPHTTPLIB_ZLIB_SUPPORT>
243239
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:CPPHTTPLIB_OPENSSL_SUPPORT>
244240
$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>,$<BOOL:${HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN}>>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN>
245-
$<$<BOOL:${HTTPLIB_IS_USING_SELECT}>:CPPHTTPLIB_USE_SELECT>
246241
)
247242

248243
# CMake configuration files installation directory

README.md

-5
Original file line numberDiff line numberDiff line change
@@ -872,11 +872,6 @@ res->body; // Compressed data
872872

873873
```
874874

875-
Use `select()` instead of `poll()`
876-
----------------------------------
877-
878-
cpp-httplib defaults to the widely supported `poll()` system call. If your OS lacks support for `poll()`, define `CPPHTTPLIB_USE_SELECT` to use `select()` instead.
879-
880875
Unix Domain Socket Support
881876
--------------------------
882877

httplib.h

+2-82
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,6 @@
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

201195
using socket_t = SOCKET;
202196
using 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

32683255
template <bool Read>
32693256
inline 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

32993266
inline 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

33073274
inline 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

33643298
inline 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

meson.build

+1-10
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,17 @@ project(
1616
meson_version: '>=0.62.0'
1717
)
1818

19-
cxx = meson.get_compiler('cpp')
20-
2119
# Check just in case downstream decides to edit the source
2220
# and add a project version
2321
version = meson.project_version()
2422
if version == 'undefined'
23+
cxx = meson.get_compiler('cpp')
2524
version = cxx.get_define('CPPHTTPLIB_VERSION',
2625
prefix: '#include <httplib.h>',
2726
include_directories: include_directories('.')).strip('"')
2827
assert(version != '', 'failed to get version from httplib.h')
2928
endif
3029

31-
if cxx.has_function('poll', prefix: '#include <poll.h>')
32-
# Use poll if present
33-
add_project_arguments('-DCPPHTTPLIB_USE_POLL', language: 'cpp')
34-
elif cxx.has_function('select', prefix: '#include <sys/select.h>')
35-
# Use select otherwise
36-
add_project_arguments('-DCPPHTTPLIB_USE_SELECT', language: 'cpp')
37-
endif
38-
3930
deps = [dependency('threads')]
4031
args = []
4132

test/Makefile

-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
CXX = clang++
22
CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address
33

4-
ifeq ($(SELECT_IMPL),select)
5-
CXXFLAGS += -DCPPHTTPLIB_USE_SELECT
6-
endif
7-
84
PREFIX ?= $(shell brew --prefix)
95

106
OPENSSL_DIR = $(PREFIX)/opt/openssl@3

0 commit comments

Comments
 (0)