Skip to content

Commit 6e73a63

Browse files
Make poll() the default (#2065)
* Make poll() the default select() can still be enabled by defining CPPHTTPLIB_USE_SELECT. * Run tests with select() and poll()
1 parent cdc2230 commit 6e73a63

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

.github/workflows/test.yaml

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,21 @@ 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']
4649
steps:
4750
- name: checkout
4851
uses: actions/checkout@v4
4952
- name: install libraries
5053
run: sudo apt-get update && sudo apt-get install -y libbrotli-dev libcurl4-openssl-dev
5154
- name: build and run tests
55+
env:
56+
SELECT_IMPL: ${{ matrix.select_impl }}
5257
run: cd test && make
5358
- name: run fuzz test target
59+
env:
60+
SELECT_IMPL: ${{ matrix.select_impl }}
5461
run: cd test && make fuzz_test
5562

5663
macos:
@@ -60,12 +67,19 @@ jobs:
6067
(github.event_name == 'pull_request' &&
6168
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
6269
(github.event_name == 'workflow_dispatch' && github.event.inputs.test_macos == 'true')
70+
strategy:
71+
matrix:
72+
select_impl: ['select', 'poll']
6373
steps:
6474
- name: checkout
6575
uses: actions/checkout@v4
6676
- name: build and run tests
77+
env:
78+
SELECT_IMPL: ${{ matrix.select_impl }}
6779
run: cd test && make
6880
- name: run fuzz test target
81+
env:
82+
SELECT_IMPL: ${{ matrix.select_impl }}
6983
run: cd test && make fuzz_test
7084

7185
windows:
@@ -75,6 +89,9 @@ jobs:
7589
(github.event_name == 'pull_request' &&
7690
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
7791
(github.event_name == 'workflow_dispatch' && github.event.inputs.test_windows == 'true')
92+
strategy:
93+
matrix:
94+
select_impl: ['select', 'poll']
7895
steps:
7996
- name: Prepare Git for Checkout on Windows
8097
run: |
@@ -96,16 +113,33 @@ jobs:
96113
choco install openssl
97114
98115
- name: Configure CMake with SSL
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
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' }}
100125
- name: Build with with SSL
101-
run: cmake --build build --config Release
126+
run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine /nologo
102127
- name: Run tests with SSL
103128
run: ctest --output-on-failure --test-dir build -C Release
104129

105130
- name: Configure CMake without SSL
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
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' }}
107141
- name: Build without SSL
108-
run: cmake --build build-no-ssl --config Release
142+
run: cmake --build build-no-ssl --config Release -- /v:m /clp:ShowCommandLine /nologo
109143
- name: Run tests without SSL
110144
run: ctest --output-on-failure --test-dir build-no-ssl -C Release
111145
env:

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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()
1112
* HTTPLIB_COMPILE (default off)
1213
* HTTPLIB_INSTALL (default on)
1314
* HTTPLIB_TEST (default off)
@@ -46,6 +47,7 @@
4647
* HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled.
4748
* HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled.
4849
* 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().
4951
* HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only.
5052
* HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include).
5153
* HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so).
@@ -101,6 +103,7 @@ option(HTTPLIB_TEST "Enables testing and builds tests" OFF)
101103
option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF)
102104
option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON)
103105
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)
104107
# Defaults to static library
105108
option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF)
106109
if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE)
@@ -112,6 +115,7 @@ endif()
112115
# Set some variables that are used in-tree and while building based on our options
113116
set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE})
114117
set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN ${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN})
118+
set(HTTPLIB_IS_USING_SELECT ${HTTPLIB_USE_SELECT})
115119

116120
# Threads needed for <thread> on some systems, and for <pthread.h> on Linux
117121
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
@@ -238,6 +242,7 @@ target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
238242
$<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:CPPHTTPLIB_ZLIB_SUPPORT>
239243
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:CPPHTTPLIB_OPENSSL_SUPPORT>
240244
$<$<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>
241246
)
242247

243248
# CMake configuration files installation directory

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,10 +872,10 @@ res->body; // Compressed data
872872

873873
```
874874

875-
Use `poll` instead of `select`
876-
------------------------------
875+
Use `select()` instead of `poll()`
876+
----------------------------------
877877

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`.
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.
879879

880880
Unix Domain Socket Support
881881
--------------------------

httplib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@
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+
148154
/*
149155
* Headers
150156
*/

test/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
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+
48
PREFIX ?= $(shell brew --prefix)
59

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

0 commit comments

Comments
 (0)