Compare commits

...
22 Commits
Author SHA1 Message Date
yhirose 7800c3dc5b Release v0.67.0 2026-11-18 12:21:49 -05:00
Pascal baba35b8d3 End a read() assembling fragments once close() is called (#2683)
A read() assembling a fragmented message looped over continuation
frames without looking at closed_, which the loop over frames checks.
A peer that kept sending fragments, empty ones included, never let the
read reach a wait, so a close() from another thread could not end it.
The read now ends with Fail once the connection is closed.
2026-11-18 12:13:13 -05:00
yhirose 92435b31b3 Let Server::stop() end a WebSocket handler's read() (#2682)
stop() takes the listening socket away, and listen() then waits for every
handler to return. A WebSocket handler waiting in read() on a client that
stays connected never learned of the stop: with no read timeout it waited
for the 300 s backstop, and with one it got Timeout back on an open
connection, so a `while (ws.read(msg) != Fail)` loop went on forever and
listen() with it.

The server now hands each WebSocket a predicate for its own stop, and the
WebSocket's read interrupt answers to it as it does to a pong timeout. A
read cut short while the server stops, between messages or in the middle
of a fragmented one, sends a Close with 1001 Going Away and returns Fail.
That close does not wait for the peer's reply, so a stop is not held up
per connection.

A server WebSocket's reads now wait in the interruptible slices whatever
the heartbeat settings, as keep-alive connections already do for the same
check.
2026-11-18 07:56:00 -05:00
Pascalandyhirose 5a956da084 Let ClientImpl::stop() end a connection being opened (#2668)
* Let ClientImpl::stop() end a connect or a TLS handshake

stop() took socket_mutex_, which send_() and open_stream() hold while
they open the connection, so a stop() made during a connect, a TLS
handshake or the CONNECT of a proxy tunnel waited for the connection
to open or to time out, 300 seconds by default, for a server that
never answers.

stop() no longer waits for the mutex while a connection opens: it
cancels the connection phase by a compare-exchange from Connecting,
and the end of the phase reads the cancel in the same exchange that
takes the state back to Idle, so a cancel stop() accepts is never
lost. stop() also sets stop_requested_: the wait of the connect and
the waits of the handshake, in every TLS backend, poll it in slices of
100 ms, as do the reads of the CONNECT of a proxy tunnel. A request or
a stream canceled there reports Error::Canceled, before it sends
anything or takes the socket. A request opened after stop() clears the
flag as before, and a stream now does too. A negative connection
timeout still waits without end in the interruptible connect wait.

* Add ClientImpl::set_interrupt(), a stop the caller holds

stop() ends what is in flight, and a request or a stream opened after
it clears the flag, so a stop() made just before a request goes out is
lost.

set_interrupt() gives the client a predicate its caller owns, polled
wherever stop_requested_ is: the connect, the TLS handshake, the
CONNECT of a proxy tunnel and the reads of a request, and the head of
a stream too. The client of a cross-host redirect keeps it. A request
or a stream refuses to go out once it holds, so a stop the caller
holds is never lost, whenever it comes. A call blocked in the system,
a send() of a large body or a TLS read inside a record, still ends
through stop() alone, which shuts the socket down.

A timeout past what the clock reaches now waits without end instead of
expiring at once, so such a wait still ends on the interrupt.

* test: create the backlog sockets with SOCK_NONBLOCK

detail::set_nonblocking lives in httplib.cc in the split build, so
test_split did not compile. The test only runs on Linux, where
SOCK_NONBLOCK makes the socket nonblocking at creation.

* Open the connection without socket_mutex_ instead

Rework the change so that send_() and open_stream_() open the
connection without holding socket_mutex_, as requests are already
serialized by request_mutex_. stop() then never waits for a connection
being opened: it always sets stop_requested_, which the connect wait,
the TLS handshake, the CONNECT and the wait for the head of a stream
poll, and the call ends with Error::Canceled before it sends anything.
This replaces the connection state machine and the try_lock loop in
stop().

The flag is cleared once, as a send() or an open_stream() starts, so a
retry inside the call no longer drops a stop() it already got.

Drop set_interrupt() for now: a predicate held per client cannot be
shared, e.g. by an SSEClient that borrows the caller's Client. Also drop
StopEndsATlsReadInsideARecord, which checked existing behavior that
does not hold on Windows, and share the listener setup of the tests.

Keep the deadline_after() fix: a timeout past what the clock reaches
waits forever instead of overflowing into an immediate timeout.

---------

Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
2026-11-17 22:40:03 -05:00
metsw24-maxandyhirose cd446a4e01 drop a passed-through Content-Length when the client reframes the body (#2679)
* drop a passed-through Content-Length when the client reframes the body

send_with_content_provider_and_receiver() kept a Content-Length the
caller passed through after the library reframed the body: it compresses
a buffered body under set_compress(true), or streams a provider without a
length as chunked. prepare_default_headers() only adds a Content-Length
when none is present, so the stale value went out describing the original
body. With compression the server reads only that many bytes of the
longer gzip stream and the rest desyncs the next request on a kept-alive
connection; with a chunked provider the client emits both Content-Length
and Transfer-Encoding. Erase it once the library owns the framing so
write_request() emits the matching length, or none for a chunked body.
A plain, uncompressed body keeps the caller's value.

* Document the dropped Content-Length and tidy the new tests

Note in the README that a per-request Content-Length is dropped when the library compresses or chunks the body; a default header is not. Shorten the comments, use HOST like the neighbouring tests, and start the chunked test's Content-Length count at a value the handler has to overwrite.

---------

Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
2026-11-17 22:38:45 -05:00
yhirose eb364f1167 Mark the test root CA as a CA explicitly instead of picking a cert version by openssl version (#2681)
gen-certs.sh and test/CMakeLists.txt chose between -x509v1 and -x509 by the
openssl version. When the default openssl changed (macOS runner image
20261112), the root was generated as v3 without basicConstraints, openssl
rejected it as 'invalid CA certificate', and every client certificate test
failed. Add basicConstraints CA:TRUE to test.rootCA.conf, drop the version
branching, and verify the client certs against the root in gen-certs.sh.
2026-11-17 21:39:31 -05:00
yhirose 748dbfade6 Bound the wait of WebSocket::close() as a whole (#2678)
close() waited for the peer's Close reply with a read timeout that
applied to each read, so a peer that kept sending other frames, or sent
one frame a byte at a time, held close() for as long as it liked.

Bound the wait as a whole. close() sets the stream's max timeout, which
shortens each wait to what is left, and stops reading frames at the
deadline, which matters when the peer keeps the socket full and no wait
happens at all. WebSocketSSLStream gains set_max_timeout(), the one
stream a WebSocket uses that lacked it.

A read() running on another thread when close() is called still waits
for the reply up to its own read timeout, as documented. The docs now
say how to bound it.
2026-11-17 08:11:39 -05:00
Graeme Smecher d7b0d4bedb Drain the socket before closing a TLS connection (#2673)
Server::process_and_close_socket() half-closes the socket and drains what
the peer is still sending before the final close (#2534), so that a close
with unread data, or with bytes still in flight, is not answered with a
RST. A RST can make the peer's stack discard a response it has received
but not yet read, as Windows does.

SSLServer::process_and_close_socket() still closed outright. After a
completed handshake, shut down TLS as before (sending close_notify when
the exchange completed) and then drain the same way. A failed handshake
closes outright as before.

Fix #2672
2026-11-16 23:53:56 -05:00
yhirose 4af42e1907 Don't reuse a client connection with unread data on it
A kept-alive connection was reused while bytes that belong to no
request were waiting on it, and the next request read them as its
response. Such bytes are not a response (RFC 9112 9.2). A server may,
for one, send a 408 before it closes an idle connection.

Close the connection when they show up: after a response, if bytes
past it are already buffered in the stream or the TLS library, and
before reuse, if anything is readable on the socket. As in curl, the
reuse check no longer asks the TLS library whether the peer closed, so
it is the same for every TLS backend. A TLS message on an idle
connection, such as a late session ticket, now costs a new connection.
2026-11-16 23:41:25 -05:00
yhirose 1b40ce850b Close the tunnel when the TLS handshake through a proxy fails
SSLClient::setup_proxy_connection() left the socket open when the TLS
handshake inside the CONNECT tunnel failed, so the client kept a
connection with no TLS session on it. Only the next request's reuse
check closed it, because tls::is_peer_closed() reports a missing
session as closed. Close it where the handshake fails, as
ensure_socket_connection() does for a direct connection.
2026-11-16 23:41:25 -05:00
metsw24-maxandyhirose f57672b390 take the request lock in open_stream_ before reusing the socket (#2669)
* take the request lock in open_stream_ before reusing the socket

* Hold the request lock only while open_stream_ takes the socket

The handle owns the connection once the socket is transferred to it, so
holding request_mutex_ while the stream wrote its request and read the
response head only made requests on other threads wait for the stream.
Also shorten the comments.

---------

Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
2026-11-16 22:08:13 -05:00
metsw24-maxandyhirose 654fd3546c resolve junctions and symlinks in canonicalize_path on Windows (#2670)
* resolve junctions and symlinks in canonicalize_path on Windows

* Shorten the canonicalize_path comment

---------

Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
2026-11-16 21:19:39 -05:00
yhirose 546e821c65 Add server_mount_fuzzer for static file serving (#2666)
Fuzz Server::handle_file_request through a mount point backed by a temporary directory that holds symlinks escaping the mount root, one of them into a sibling directory sharing its name prefix. Abort if a token stored outside the root appears in a response. The seeds are based on those of #2661.

Move FuzzedStream into fuzzed_stream.h so that both server fuzzers share it, and add FuzzableServer::MarkAsRunning(), without which a server that is not listening writes no file.
2026-11-12 21:52:33 -05:00
yhirose 53a7f4964e Add sse_parser_fuzzer for detail::SSEParser (#2665)
Feed the event stream to the parser in chunks and abort if the reported events, ids and retries differ from parsing it in one piece. Also run with a length limit to cover the early-stop path.
2026-11-12 21:11:48 -05:00
Arthur Chan 8c5fd6aa08 OSS-Fuzz: Improve url_parser_fuzzer with more cases (#2664)
Add cases for relative location resolution, dot segment removal, URI encoding and decoding, and filename sanitization.
2026-11-12 20:47:26 -05:00
Arthur Chan a2ebab183d OSS-Fuzz: Add new fuzzer/options targets websocket (#2663)
Add websocket_fuzzer covering frame reading, UTF-8 and close payload validation, and the upgrade response parser. Add websocket_fuzzer.options to raise max_len above the 64 KiB frame length boundary.
2026-11-12 20:47:22 -05:00
yhirose a39e081386 Test SSEParser directly instead of through a server
Move the SSE parsing tests from a fixture that started a server and an
SSEClient for every case to unit tests of detail::SSEParser, and turn
NonDigitRetryFieldIgnored, which slept for 500ms, into one of them. Add
tests that feed each stream split at every position and byte by byte,
and that cover the max event length and an event left incomplete at the
end of the stream.
2026-11-12 19:07:54 -05:00
yhirose 07142256ed Extract SSE event stream parsing into detail::SSEParser
Move the line splitting and event assembly out of
SSEClient::run_event_loop() into a parser that is fed chunks and reports
events, ids and retry values through handlers. The event loop is left
with I/O and reconnection, and the parser can be driven without a
server, which the following test and fuzzer changes rely on.

No behavior change.
2026-11-12 18:27:58 -05:00
userandastunm cc7a30f12f Preserve aliased keys when erasing entries (#2659)
Co-authored-by: astunm <astunm@users.noreply.github.com>
2026-11-11 14:32:09 -05:00
yhirose cdcd2ac6e6 Release v0.66.0 2026-11-10 17:19:39 -05:00
yhirose bd23a7b179 Keep the job queued when a dynamic worker thread cannot be created
ThreadPool::enqueue constructed the dynamic thread without handling
std::system_error. When thread creation failed (e.g. EAGAIN under a
pids limit), the exception escaped the accept loop and destroying the
pool's joinable threads called std::terminate. Treat the failure as
"no spare capacity" and leave the job for an existing worker.
2026-11-10 16:54:53 -05:00
metsw24-max 6d8dec3c3e Keep a redirect on the unix socket in ClientImpl::redirect (#2658)
A client using set_address_family(AF_UNIX) took the host of an absolute Location as a socket path, so a redirect to http://localhost/next failed and one to http://other.sock/next went to ./other.sock. Every hop now stays on the client's socket, as with curl's --unix-socket.

The same host is still decided from the Location, as curl does: only localhost (the Host this client sends) counts as this client, so a redirect to any other host goes through the cross-host client and drops Authorization and Cookie. That client reaches the original socket through addr_map_, and a host the caller mapped with set_direct_hostname_addr_map keeps its mapping. An https Location runs TLS over the socket with the Location's host.

The mapped-target lookup shared with make_connect_request_target is factored into detail::addr_map_target.
2026-11-07 10:22:00 -05:00
33 changed files with 2098 additions and 623 deletions

No files matched your search

+1 -1
View File
@@ -568,7 +568,7 @@ Choose sizes that account for both your expected HTTP load and the maximum numbe
A single `WebSocket` (server-side) or `WebSocketClient` handle is shared by three potential callers: the thread running your handler (or holding the client), the heartbeat thread, and, if your code does its own thing, a separate thread calling `send()`/`close()` while another thread is blocked in `read()`. A single `WebSocket` (server-side) or `WebSocketClient` handle is shared by three potential callers: the thread running your handler (or holding the client), the heartbeat thread, and, if your code does its own thing, a separate thread calling `send()`/`close()` while another thread is blocked in `read()`.
**Supported**: calling `read()` from one thread while calling `send()`/`close()` from another. This is the common pattern for a client that reads incoming messages in a loop on one thread and sends from elsewhere (e.g. a UI thread). A message that is in flight when `close()` is called still arrives intact; `close()` sends the Close frame and returns, leaving the connection's read side to the thread that owns it, so it does not block waiting for the peer's Close reply in that case. The heartbeat thread's automatic pings use the same `send()` path internally, so they are safe to run concurrently with your `read()` loop too — for `wss://` this requires every TLS call on a connection to be serialized internally, which cpp-httplib does for you. **Supported**: calling `read()` from one thread while calling `send()`/`close()` from another. This is the common pattern for a client that reads incoming messages in a loop on one thread and sends from elsewhere (e.g. a UI thread). A message that is in flight when `close()` is called still arrives intact; `close()` sends the Close frame and returns, leaving the connection's read side to the thread that owns it, so it does not block waiting for the peer's Close reply in that case. That thread's `read()` keeps waiting for the reply until its own read timeout (by default none on a client, 300 seconds on a server); to bound it, set one with `set_read_timeout()` before the read starts. The heartbeat thread's automatic pings use the same `send()` path internally, so they are safe to run concurrently with your `read()` loop too — for `wss://` this requires every TLS call on a connection to be serialized internally, which cpp-httplib does for you.
**Not supported**: calling `read()` from two threads at the same time on the same handle. The calls are serialized rather than left to corrupt each other, but which thread receives which message is unspecified, so there is nothing useful to build on it. **Not supported**: calling `read()` from two threads at the same time on the same handle. The calls are serialized rather than left to corrupt each other, but which thread receives which message is unspecified, so there is nothing useful to build on it.
+3 -1
View File
@@ -1172,7 +1172,7 @@ cli.set_default_headers({
auto res = cli.Get("/hi"); auto res = cli.Get("/hi");
``` ```
A `Content-Length` among these headers is sent in place of the one the library would send, so it has to match the body as written. A `Transfer-Encoding` is sent in addition to the library's own framing, so leave it out. Leave both out when passing on the headers of another message, as a proxy does. A `Content-Length` among these headers is sent in place of the one the library would send, so it has to match the body as written. One passed with the request is dropped when the library compresses the body or sends it chunked. A `Transfer-Encoding` is sent in addition to the library's own framing, so leave it out. Leave both out when passing on the headers of another message, as a proxy does.
### POST ### POST
@@ -1716,6 +1716,8 @@ cli.set_address_family(AF_UNIX);
This library automatically sets the Host header to "localhost" for Unix socket connections, similar to curl's behavior. This library automatically sets the Host header to "localhost" for Unix socket connections, similar to curl's behavior.
With `set_follow_location(true)`, a redirect is followed over the same socket, whatever host the `Location` names, as curl's `--unix-socket` does. A host mapped with `set_direct_hostname_addr_map` goes to its mapped socket instead. Only a `Location` naming `localhost` counts as the same host; any other host is treated as cross-host, so `Authorization` and `Cookie` are not sent to it.
URI Encoding/Decoding Utilities URI Encoding/Decoding Utilities
------------------------------- -------------------------------
+1 -1
View File
@@ -4,7 +4,7 @@ langs = ["en", "ja"]
[site] [site]
title = "cpp-httplib" title = "cpp-httplib"
version = "0.65.0" version = "0.67.0"
hostname = "https://yhirose.github.io" hostname = "https://yhirose.github.io"
base_path = "/cpp-httplib" base_path = "/cpp-httplib"
footer_message = "© 2026 Yuji Hirose. All rights reserved." footer_message = "© 2026 Yuji Hirose. All rights reserved."
@@ -56,4 +56,4 @@ When you call `stop()`, new connections are refused, but handlers that are alrea
> **Warning:** There's a wait between calling `stop()` and `listen()` returning — it's the time in-flight requests take to finish. To enforce a timeout, you'll need to add your own shutdown timer in application code. > **Warning:** There's a wait between calling `stop()` and `listen()` returning — it's the time in-flight requests take to finish. To enforce a timeout, you'll need to add your own shutdown timer in application code.
> **Note:** `stop()` doesn't interrupt WebSocket handlers. A handler looping on `read()` keeps running as long as its peer stays connected, and `listen()` doesn't return until it ends. Keep track of open WebSocket connections and `close()` them on shutdown, e.g. with `CloseStatus::GoingAway`. > **Note:** `stop()` doesn't interrupt WebSocket handlers. A handler looping on `read()` keeps running as long as its peer stays connected, and `listen()` doesn't return until it ends. Keep track of open WebSocket connections and `close()` them on shutdown, e.g. with `CloseStatus::GoingAway`. The handler's `read()` then waits for the peer's Close reply up to its read timeout (300 seconds by default). To shorten that, call `set_read_timeout()` in the handler before the loop; `read()` then also returns `Timeout` whenever the peer stays idle that long, so the loop has to handle it.
@@ -56,4 +56,4 @@ int main() {
> **Warning:** `stop()`を呼んでから`listen()`が戻るまでには、処理中のリクエストが終わるのを待つ時間がかかります。タイムアウトを強制したい場合は、シャットダウン用のタイマーを別途用意するなど、アプリケーション側の工夫が必要です。 > **Warning:** `stop()`を呼んでから`listen()`が戻るまでには、処理中のリクエストが終わるのを待つ時間がかかります。タイムアウトを強制したい場合は、シャットダウン用のタイマーを別途用意するなど、アプリケーション側の工夫が必要です。
> **Note:** `stop()`はWebSocketハンドラを中断しません。`read()`をループで回しているハンドラは相手がつながっている限り動き続け、それが終わるまで`listen()`も戻りません。開いているWebSocket接続を記録しておき、シャットダウン時に`close()`(`CloseStatus::GoingAway`など)で閉じてください。 > **Note:** `stop()`はWebSocketハンドラを中断しません。`read()`をループで回しているハンドラは相手がつながっている限り動き続け、それが終わるまで`listen()`も戻りません。開いているWebSocket接続を記録しておき、シャットダウン時に`close()`(`CloseStatus::GoingAway`など)で閉じてください。そのあとハンドラの`read()`は、相手のClose応答を読み込みタイムアウト(既定は300秒)まで待ちます。短くしたい場合は、ループの前に`set_read_timeout()`を呼んでください。その場合、相手が黙っている間は`read()`が`Timeout`を返すので、ループ側で扱う必要があります。
+477 -265
View File
File diff suppressed because it is too large. Load diff
+2 -7
View File
@@ -37,11 +37,6 @@ file(
) )
if(HTTPLIB_IS_USING_OPENSSL) if(HTTPLIB_IS_USING_OPENSSL)
if (OPENSSL_VERSION VERSION_LESS "3.2.0")
set(OPENSSL_X509_FLAG "-x509")
else()
set(OPENSSL_X509_FLAG "-x509v1")
endif()
find_program(OPENSSL_COMMAND find_program(OPENSSL_COMMAND
NAMES openssl NAMES openssl
PATHS ${OPENSSL_INCLUDE_DIR}/../bin PATHS ${OPENSSL_INCLUDE_DIR}/../bin
@@ -61,7 +56,7 @@ if(HTTPLIB_IS_USING_OPENSSL)
COMMAND_ERROR_IS_FATAL ANY COMMAND_ERROR_IS_FATAL ANY
) )
execute_process( execute_process(
COMMAND ${OPENSSL_COMMAND} req ${OPENSSL_X509_FLAG} -new -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN COMMAND ${OPENSSL_COMMAND} req -x509 -new -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND_ERROR_IS_FATAL ANY COMMAND_ERROR_IS_FATAL ANY
) )
@@ -72,7 +67,7 @@ if(HTTPLIB_IS_USING_OPENSSL)
COMMAND_ERROR_IS_FATAL ANY COMMAND_ERROR_IS_FATAL ANY
) )
execute_process( execute_process(
COMMAND ${OPENSSL_COMMAND} req ${OPENSSL_X509_FLAG} -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.rootCA.conf -key rootCA.key.pem -days 1024 COMMAND ${OPENSSL_COMMAND} req -x509 -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.rootCA.conf -key rootCA.key.pem -days 1024
OUTPUT_FILE rootCA.cert.pem OUTPUT_FILE rootCA.cert.pem
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND_ERROR_IS_FATAL ANY COMMAND_ERROR_IS_FATAL ANY
+1 -1
View File
@@ -260,7 +260,7 @@ endif
test_benchmark : test_benchmark.cc ../httplib.h Makefile test_benchmark : test_benchmark.cc ../httplib.h Makefile
$(CXX) -o $@ -I.. $(CXXFLAGS) test_benchmark.cc gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(BENCHMARK_LIBS) $(CXX) -o $@ -I.. $(CXXFLAGS) test_benchmark.cc gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(BENCHMARK_LIBS)
test_websocket_heartbeat : test_websocket_heartbeat.cc ../httplib.h Makefile test_websocket_heartbeat : test_websocket_heartbeat.cc ../httplib.h Makefile cert.pem
$(CXX) -o $@ -I.. $(CXXFLAGS) test_websocket_heartbeat.cc $(TEST_ARGS) $(CXX) -o $@ -I.. $(CXXFLAGS) test_websocket_heartbeat.cc $(TEST_ARGS)
@file $@ @file $@
+13 -2
View File
@@ -13,14 +13,15 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
BROTLI_DIR = /usr/local/opt/brotli BROTLI_DIR = /usr/local/opt/brotli
# BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec # BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
FUZZERS = server_fuzzer url_parser_fuzzer header_parser_fuzzer client_fuzzer multipart_parser_fuzzer FUZZERS = server_fuzzer url_parser_fuzzer header_parser_fuzzer client_fuzzer multipart_parser_fuzzer websocket_fuzzer sse_parser_fuzzer server_mount_fuzzer
# Runs all the tests and also fuzz tests against seed corpus. # Runs all the tests and also fuzz tests against seed corpus.
all : $(FUZZERS) all : $(FUZZERS)
./server_fuzzer corpus/* ./server_fuzzer corpus/*
./server_mount_fuzzer corpus_mount/*
# Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use. # Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use.
server_fuzzer : server_fuzzer.cc ../../httplib.h server_fuzzer : server_fuzzer.cc fuzzed_stream.h ../../httplib.h
# $(CXX) $(CXXFLAGS) -o $@ $< -Wl,-Bstatic $(OPENSSL_SUPPORT) -Wl,-Bdynamic -ldl $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread # $(CXX) $(CXXFLAGS) -o $@ $< -Wl,-Bstatic $(OPENSSL_SUPPORT) -Wl,-Bdynamic -ldl $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl $(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
zip -q -r server_fuzzer_seed_corpus.zip corpus zip -q -r server_fuzzer_seed_corpus.zip corpus
@@ -38,5 +39,15 @@ url_parser_fuzzer : url_parser_fuzzer.cc ../../httplib.h
multipart_parser_fuzzer : multipart_parser_fuzzer.cc ../../httplib.h multipart_parser_fuzzer : multipart_parser_fuzzer.cc ../../httplib.h
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl $(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
websocket_fuzzer : websocket_fuzzer.cc ../../httplib.h
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
sse_parser_fuzzer : sse_parser_fuzzer.cc ../../httplib.h
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
server_mount_fuzzer : server_mount_fuzzer.cc fuzzed_stream.h ../../httplib.h
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
zip -q -r server_mount_fuzzer_seed_corpus.zip corpus_mount
clean: clean:
rm -f server_fuzzer pem *.0 *.o *.1 *.srl *.zip rm -f server_fuzzer pem *.0 *.o *.1 *.srl *.zip
+3
View File
@@ -0,0 +1,3 @@
HEAD /mnt/a.txt HTTP/1.1
Host: x
+4
View File
@@ -0,0 +1,4 @@
GET /mnt/a.txt HTTP/1.1
Host: x
If-Modified-Since: Mon, 01 Jan 2024 00:00:00 GMT
+3
View File
@@ -0,0 +1,3 @@
GET /mnt/index.html HTTP/1.1
Host: x
+4
View File
@@ -0,0 +1,4 @@
GET /mnt/index.html HTTP/1.1
Host: x
If-None-Match: W/"bogusetag"
+3
View File
@@ -0,0 +1,3 @@
GET /mnt/link-in.txt HTTP/1.1
Host: x
+3
View File
@@ -0,0 +1,3 @@
GET /mnt/link-out.txt HTTP/1.1
Host: x
@@ -0,0 +1,3 @@
GET /mnt/link-sibling.txt HTTP/1.1
Host: x
+3
View File
@@ -0,0 +1,3 @@
GET /mnt/missing.txt HTTP/1.1
Host: x
+4
View File
@@ -0,0 +1,4 @@
GET /mnt/a.txt HTTP/1.1
Host: x
Range: bytes=0-3
+3
View File
@@ -0,0 +1,3 @@
GET /mnt/sub/ HTTP/1.1
Host: x
@@ -0,0 +1,3 @@
GET /mnt/../../../../etc/passwd HTTP/1.1
Host: x
@@ -0,0 +1,3 @@
GET /mnt/%2e%2e/secret.txt HTTP/1.1
Host: x
+84
View File
@@ -0,0 +1,84 @@
#pragma once
#include <cstdint>
#include <httplib.h>
class FuzzedStream : public httplib::Stream {
public:
FuzzedStream(const uint8_t *data, size_t size)
: data_(data), size_(size), read_pos_(0) {}
ssize_t read(char *ptr, size_t size) override {
if (size + read_pos_ > size_) { size = size_ - read_pos_; }
memcpy(ptr, data_ + read_pos_, size);
read_pos_ += size;
// Like SocketStream, pull in more than was asked for, which the line
// reader then takes through buffered_data(), so both of its paths run.
buffered_end_ =
(std::max)(buffered_end_, (std::min)(size_, read_pos_ + 4096));
return static_cast<ssize_t>(size);
}
const char *buffered_data(size_t &size) const override {
size = buffered_end_ > read_pos_ ? buffered_end_ - read_pos_ : 0;
return size ? reinterpret_cast<const char *>(data_ + read_pos_) : nullptr;
}
void consume_buffered(size_t size) override { read_pos_ += size; }
ssize_t write(const char *ptr, size_t size) override {
response_.append(ptr, size);
return static_cast<int>(size);
}
ssize_t write(const char *ptr) { return write(ptr, strlen(ptr)); }
ssize_t write(const std::string &s) { return write(s.data(), s.size()); }
bool is_readable() const override { return true; }
bool wait_readable() const override { return true; }
bool wait_writable() const override { return true; }
void get_remote_ip_and_port(std::string &ip, int &port) const override {
ip = "127.0.0.1";
port = 8080;
}
void get_local_ip_and_port(std::string &ip, int &port) const override {
ip = "127.0.0.1";
port = 8080;
}
socket_t socket() const override { return 0; }
time_t duration() const override { return 0; };
const std::string &response() const { return response_; }
private:
const uint8_t *data_;
size_t size_;
size_t read_pos_;
size_t buffered_end_ = 0;
std::string response_;
};
class FuzzableServer : public httplib::Server {
public:
// A server that is not listening counts as shutting down, and then writes
// no file. The socket is never used.
void MarkAsRunning() { svr_sock_ = 0; }
void ProcessFuzzedRequest(FuzzedStream &stream) {
bool connection_close = false;
process_request(stream,
/*remote_addr=*/"",
/*remote_port =*/0,
/*local_addr=*/"",
/*local_port =*/0,
/*last_connection=*/false, connection_close, nullptr);
}
};
+1 -76
View File
@@ -1,79 +1,4 @@
#include <cstdint> #include "fuzzed_stream.h"
#include <httplib.h>
class FuzzedStream : public httplib::Stream {
public:
FuzzedStream(const uint8_t *data, size_t size)
: data_(data), size_(size), read_pos_(0) {}
ssize_t read(char *ptr, size_t size) override {
if (size + read_pos_ > size_) { size = size_ - read_pos_; }
memcpy(ptr, data_ + read_pos_, size);
read_pos_ += size;
// Like SocketStream, pull in more than was asked for, which the line
// reader then takes through buffered_data(), so both of its paths run.
buffered_end_ =
(std::max)(buffered_end_, (std::min)(size_, read_pos_ + 4096));
return static_cast<ssize_t>(size);
}
const char *buffered_data(size_t &size) const override {
size = buffered_end_ > read_pos_ ? buffered_end_ - read_pos_ : 0;
return size ? reinterpret_cast<const char *>(data_ + read_pos_) : nullptr;
}
void consume_buffered(size_t size) override { read_pos_ += size; }
ssize_t write(const char *ptr, size_t size) override {
response_.append(ptr, size);
return static_cast<int>(size);
}
ssize_t write(const char *ptr) { return write(ptr, strlen(ptr)); }
ssize_t write(const std::string &s) { return write(s.data(), s.size()); }
bool is_readable() const override { return true; }
bool wait_readable() const override { return true; }
bool wait_writable() const override { return true; }
void get_remote_ip_and_port(std::string &ip, int &port) const override {
ip = "127.0.0.1";
port = 8080;
}
void get_local_ip_and_port(std::string &ip, int &port) const override {
ip = "127.0.0.1";
port = 8080;
}
socket_t socket() const override { return 0; }
time_t duration() const override { return 0; };
private:
const uint8_t *data_;
size_t size_;
size_t read_pos_;
size_t buffered_end_ = 0;
std::string response_;
};
class FuzzableServer : public httplib::Server {
public:
void ProcessFuzzedRequest(FuzzedStream &stream) {
bool connection_close = false;
process_request(stream,
/*remote_addr=*/"",
/*remote_port =*/0,
/*local_addr=*/"",
/*local_port =*/0,
/*last_connection=*/false, connection_close, nullptr);
}
};
static FuzzableServer g_server; static FuzzableServer g_server;
+117
View File
@@ -0,0 +1,117 @@
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <random>
#include <string>
#include <vector>
#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>
#include "fuzzed_stream.h"
// Fixture layout, with <token> unknown to the fuzzer:
// root/mnt/{index.html,a.txt,data.bin,sub/index.html} served at /mnt
// root/mnt/link-in.txt -> a.txt (stays inside, may be served)
// root/mnt/link-out.txt -> ../outside.txt (escapes, must be refused)
// root/mnt/link-sibling.txt -> ../mnt_secret/secret.txt (shares the "mnt"
// prefix, must be refused)
// The response must never contain <token>.
namespace {
FuzzableServer g_server;
std::string g_root;
std::string g_token;
std::vector<std::string> g_paths; // In creation order
void check(bool ok, const char *what) {
if (!ok) {
fprintf(stderr, "fixture setup failed: %s\n", what);
abort();
}
}
void make_dir(const std::string &path) {
check(mkdir(path.c_str(), 0755) == 0, path.c_str());
g_paths.push_back(path);
}
void make_file(const std::string &path, const std::string &content) {
{
std::ofstream out(path, std::ios::binary);
out << content;
check(out.good(), path.c_str());
}
// Fixed, so that conditional requests behave the same in every run
utimbuf times = {1700000000, 1700000000};
check(utime(path.c_str(), &times) == 0, path.c_str());
g_paths.push_back(path);
}
void make_link(const std::string &target, const std::string &path) {
check(symlink(target.c_str(), path.c_str()) == 0, path.c_str());
g_paths.push_back(path);
}
std::string serve(const uint8_t *data, size_t size) {
FuzzedStream stream{data, size};
g_server.ProcessFuzzedRequest(stream);
return stream.response();
}
void check_served(const char *path) {
auto req = std::string("GET ") + path + " HTTP/1.1\r\nHost: x\r\n\r\n";
auto res = serve(reinterpret_cast<const uint8_t *>(req.data()), req.size());
// Without this, a fuzzer that serves nothing would pass its check forever
check(res.find("abcdef") != std::string::npos, path);
}
void remove_fixture() {
for (auto it = g_paths.rbegin(); it != g_paths.rend(); ++it) {
if (unlink(it->c_str()) != 0) { rmdir(it->c_str()); }
}
rmdir(g_root.c_str());
}
} // namespace
extern "C" int LLVMFuzzerInitialize(int * /*argc*/, char *** /*argv*/) {
std::random_device rd;
char token[33];
snprintf(token, sizeof(token), "%08x%08x%08x%08x", rd(), rd(), rd(), rd());
g_token = token;
auto tmpdir = getenv("TMPDIR");
auto tmpl = std::string(tmpdir && *tmpdir ? tmpdir : "/tmp") +
"/httplib-mount-XXXXXX";
check(mkdtemp(&tmpl[0]) != nullptr, tmpl.c_str());
g_root = tmpl;
atexit(remove_fixture);
auto mnt = g_root + "/mnt";
make_dir(mnt);
make_dir(mnt + "/sub");
make_dir(g_root + "/mnt_secret");
make_file(mnt + "/index.html", "<html>index</html>");
make_file(mnt + "/a.txt", "abcdef");
make_file(mnt + "/data.bin", std::string("\x01\x02\x03\x04\x05\x06", 6));
make_file(mnt + "/sub/index.html", "<html>sub</html>");
make_file(g_root + "/outside.txt", g_token);
make_file(g_root + "/mnt_secret/secret.txt", g_token);
make_link("a.txt", mnt + "/link-in.txt");
make_link("../outside.txt", mnt + "/link-out.txt");
make_link("../mnt_secret/secret.txt", mnt + "/link-sibling.txt");
g_server.MarkAsRunning();
check(g_server.set_mount_point("/mnt", mnt), "set_mount_point");
check_served("/mnt/a.txt");
check_served("/mnt/link-in.txt");
return 0;
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (serve(data, size).find(g_token) != std::string::npos) { abort(); }
return 0;
}
+24
View File
@@ -0,0 +1,24 @@
"GET"
"HEAD"
"HTTP/1.1"
"/mnt/"
"/mnt/index.html"
"/mnt/a.txt"
"/mnt/data.bin"
"/mnt/sub/"
"/mnt/link-in.txt"
"/mnt/link-out.txt"
"/mnt/link-sibling.txt"
".."
"%2e%2e"
"%2f"
"//"
"Range: bytes=0-"
"Range: bytes=0-3,5-"
"If-None-Match: "
"W/"
"If-Modified-Since: "
"If-Range: "
"Accept-Encoding: gzip"
+49
View File
@@ -0,0 +1,49 @@
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <string>
#include <httplib.h>
namespace {
// Feeds the stream in chunks and records every handler call in order
std::string parse_in_chunks(const std::string &stream, size_t chunk,
size_t max_event_length) {
std::string trace;
httplib::detail::SSEParser parser(
[&](const httplib::sse::SSEMessage &msg) {
trace += "event[" + msg.event + "|" + msg.data + "|" + msg.id + "]";
},
[&](const std::string &id) { trace += "id[" + id + "]"; },
[&](int retry_ms) { trace += "retry[" + std::to_string(retry_ms) + "]"; },
max_event_length);
for (size_t off = 0; off < stream.size(); off += chunk) {
if (!parser.parse(stream.data() + off,
std::min(chunk, stream.size() - off))) {
break;
}
}
return trace;
}
} // namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size < 2 || size > 65536) { return 0; }
// First byte selects the chunk size, the rest is the event stream
auto chunk = static_cast<size_t>(data[0]) + 1;
std::string stream(reinterpret_cast<const char *>(data + 1), size - 1);
// Without a length limit, chunking must not change what is reported
auto whole = parse_in_chunks(stream, stream.size(), 0);
auto chunked = parse_in_chunks(stream, chunk, 0);
if (whole != chunked) { std::abort(); }
// With a limit, only check that the parser stops cleanly
parse_in_chunks(stream, chunk, 64);
return 0;
}
+43 -1
View File
@@ -13,7 +13,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
size_t payload_size = size - 1; size_t payload_size = size - 1;
std::string input(payload, payload_size); std::string input(payload, payload_size);
switch (selector % 6) { switch (selector % 13) {
case 0: { case 0: {
// parse_query_text // parse_query_text
httplib::Params params; httplib::Params params;
@@ -46,6 +46,48 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
httplib::detail::is_valid_path(input); httplib::detail::is_valid_path(input);
break; break;
} }
case 6: {
// parse_url
httplib::detail::UrlComponents uc;
httplib::detail::parse_url(input, uc);
break;
}
case 7: {
// remove_dot_segments
httplib::detail::remove_dot_segments(input);
break;
}
case 8: {
// resolve_relative_location, with the input as "<location>\n<base>"
auto nl = input.find('\n');
auto location = input.substr(0, nl);
auto base = nl == std::string::npos ? std::string("/index.html")
: input.substr(nl + 1);
httplib::detail::resolve_relative_location(location, base);
break;
}
case 9: {
// encode_path
httplib::detail::encode_path(input);
break;
}
case 10: {
// encode_uri_component, encode_uri
httplib::encode_uri_component(input);
httplib::encode_uri(input);
break;
}
case 11: {
// decode_uri_component, decode_uri
httplib::decode_uri_component(input);
httplib::decode_uri(input);
break;
}
case 12: {
// sanitize_filename
httplib::sanitize_filename(input);
break;
}
} }
return 0; return 0;
+54
View File
@@ -0,0 +1,54 @@
#include <cstdint>
#include <string>
#include <httplib.h>
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size < 2) { return 0; }
auto selector = data[0];
std::string input(reinterpret_cast<const char *>(data + 1), size - 1);
switch (selector % 4) {
case 0: {
// read_websocket_frame. WebSocket::read passes the macro, close() passes
// 125, and 0 means no limit
static const size_t max_lens[] = {0, 125,
CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH};
auto expect_masked = (selector / 4) % 2 != 0;
auto max_len = max_lens[(selector / 8) % 3];
httplib::detail::BufferStream strm;
strm.write(input.data(), input.size());
auto opcode = httplib::ws::Opcode::Text;
std::string payload;
auto fin = false;
httplib::ws::impl::read_websocket_frame(strm, opcode, payload, fin,
expect_masked, max_len);
break;
}
case 1: {
// is_valid_utf8
httplib::ws::impl::is_valid_utf8(input);
break;
}
case 2: {
// is_valid_close_payload
httplib::ws::impl::is_valid_close_payload(input);
break;
}
case 3: {
// read_websocket_upgrade_response, with a fixed key since
// perform_websocket_handshake draws a random one
static const auto expected_accept =
httplib::detail::websocket_accept_key("dGhlIHNhbXBsZSBub25jZQ==");
httplib::detail::BufferStream strm;
strm.write(input.data(), input.size());
httplib::detail::WebSocketUpgradeResponse upgrade;
httplib::detail::read_websocket_upgrade_response(strm, expected_accept,
"chat, stream", upgrade);
break;
}
}
return 0;
}
+2
View File
@@ -0,0 +1,2 @@
[libfuzzer]
max_len = 70000
+5 -7
View File
@@ -1,15 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if [[ $(openssl version) =~ 3\.[2-9]\.[0-9]+ ]]; then
OPENSSL_X509_FLAG='-x509v1'
else
OPENSSL_X509_FLAG='-x509'
fi
openssl genrsa 2048 > key.pem openssl genrsa 2048 > key.pem
openssl req -new -batch -config test.conf -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem openssl req -new -batch -config test.conf -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
openssl req -x509 -config test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN openssl req -x509 -config test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN
openssl genrsa 2048 > rootCA.key.pem openssl genrsa 2048 > rootCA.key.pem
openssl req $OPENSSL_X509_FLAG -new -batch -config test.rootCA.conf -key rootCA.key.pem -days 1024 > rootCA.cert.pem openssl req -x509 -new -batch -config test.rootCA.conf -key rootCA.key.pem -days 1024 > rootCA.cert.pem
openssl genrsa 2048 > client.key.pem openssl genrsa 2048 > client.key.pem
openssl req -new -batch -config test.conf -key client.key.pem | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem openssl req -new -batch -config test.conf -key client.key.pem | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem
openssl genrsa -passout pass:test123! 2048 > key_encrypted.pem openssl genrsa -passout pass:test123! 2048 > key_encrypted.pem
@@ -47,3 +41,7 @@ openssl req -x509 -key key.pem -sha256 -days 3650 -nodes -subj "/CN=localhost" -
# "*.leftmost.example.test" matches a single label; the # "*.leftmost.example.test" matches a single label; the
# wildcard in "www.*.example.test" must not be honoured. # wildcard in "www.*.example.test" must not be honoured.
openssl req -x509 -key key.pem -sha256 -days 3650 -nodes -subj "/CN=wildcard-san" -addext "subjectAltName=DNS:*.leftmost.example.test,DNS:www.*.example.test" -out cert_wildcard_san.pem openssl req -x509 -key key.pem -sha256 -days 3650 -nodes -subj "/CN=wildcard-san" -addext "subjectAltName=DNS:*.leftmost.example.test,DNS:www.*.example.test" -out cert_wildcard_san.pem
# The client certs must chain to a root that openssl accepts as a CA; a root
# without basicConstraints CA:TRUE makes every client-cert test fail.
openssl verify -CAfile rootCA.cert.pem client.cert.pem client_encrypted.cert.pem
+1035 -256
View File
File diff suppressed because it is too large. Load diff
+6
View File
@@ -2,6 +2,7 @@
default_bits = 2048 default_bits = 2048
distinguished_name = req_distinguished_name distinguished_name = req_distinguished_name
attributes = req_attributes attributes = req_attributes
x509_extensions = v3_ca
prompt = no prompt = no
output_password = mypass output_password = mypass
@@ -16,3 +17,8 @@ emailAddress = test@email.address
[req_attributes] [req_attributes]
challengePassword = 1234 challengePassword = 1234
# Mark the root as a CA explicitly. Relying on the openssl version to emit a
# v1 certificate (or CA:TRUE by default) breaks when the default changes.
[v3_ca]
basicConstraints = critical,CA:TRUE
+139 -3
View File
@@ -1,10 +1,10 @@
// Standalone test for WebSocket automatic heartbeat. // Standalone test for WebSocket heartbeat and close() timing, compiled with
// Compiled with a 1-second ping interval so we can verify heartbeat behavior // short intervals and timeouts so the tests don't wait for the defaults.
// without waiting 30 seconds.
#define CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND 1 #define CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND 1
#define CPPHTTPLIB_WEBSOCKET_CLIENT_READ_TIMEOUT_SECOND 3 #define CPPHTTPLIB_WEBSOCKET_CLIENT_READ_TIMEOUT_SECOND 3
#define CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND 3 #define CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND 3
#define CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND 1
#include <httplib.h> #include <httplib.h>
#include "gtest/gtest.h" #include "gtest/gtest.h"
@@ -289,3 +289,139 @@ TEST_F(WebSocketHeartbeatTest, MultipleHeartbeatCycles) {
client.close(); client.close();
} }
// A raw client never answers the server's close() and sends `bytes`, `chunk`
// bytes at a time `gap` apart; close() must still return at its 1 s timeout.
static void expect_close_bounded(bool use_tls, const std::string &bytes,
size_t chunk, std::chrono::milliseconds gap) {
std::unique_ptr<Server> svr;
#ifdef CPPHTTPLIB_SSL_ENABLED
if (use_tls) { svr.reset(new SSLServer("cert.pem", "key.pem")); }
#endif
if (!svr) { svr.reset(new Server); }
svr->set_websocket_ping_interval(0);
std::atomic<bool> upgraded{false};
std::atomic<bool> closed{false};
std::chrono::steady_clock::duration close_took{};
svr->WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) {
while (!upgraded) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
auto start = std::chrono::steady_clock::now();
ws.close();
close_took = std::chrono::steady_clock::now() - start;
closed = true;
});
auto port = svr->bind_to_any_port("127.0.0.1");
ASSERT_GT(port, 0);
std::thread t([&] { svr->listen_after_bind(); });
auto se = detail::scope_exit([&] {
upgraded = true; // Releases the handler when an assertion bails out early
svr->stop();
t.join();
});
svr->wait_until_ready();
auto sock = ::socket(AF_INET, SOCK_STREAM, 0);
ASSERT_NE(INVALID_SOCKET, sock);
auto sock_se = detail::scope_exit([&] { detail::close_socket(sock); });
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(static_cast<uint16_t>(port));
::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
ASSERT_EQ(0,
::connect(sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)));
std::unique_ptr<Stream> strm;
#ifdef CPPHTTPLIB_SSL_ENABLED
tls::ctx_t ctx = nullptr;
tls::session_t session = nullptr;
auto tls_se = detail::scope_exit([&] {
if (session) { tls::free_session(session); }
if (ctx) { tls::free_context(ctx); }
});
if (use_tls) {
ctx = tls::create_client_context();
tls::set_verify_client(ctx, false); // The test certificate is self-signed
session = tls::create_session(ctx, sock);
ASSERT_TRUE(tls::connect_nonblocking(session, sock, 5, 0, nullptr));
strm.reset(new detail::SSLSocketStream(sock, session, 5, 0, 5, 0));
}
#endif
if (!strm) { strm.reset(new detail::SocketStream(sock, 5, 0, 5, 0)); }
const std::string upgrade = "GET /ws HTTP/1.1\r\n"
"Host: 127.0.0.1\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Key: AAAAAAAAAAAAAAAAAAAAAA==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n";
ASSERT_EQ(static_cast<ssize_t>(upgrade.size()),
strm->write(upgrade.data(), upgrade.size()));
std::string response;
char c;
while (response.find("\r\n\r\n") == std::string::npos &&
strm->read(&c, 1) == 1) {
response += c;
}
ASSERT_NE(std::string::npos, response.find(" 101 "));
upgraded = true;
// Without the bound, close() would wait as long as the client sends
auto give_up = std::chrono::steady_clock::now() + std::chrono::seconds(4);
while (!closed && std::chrono::steady_clock::now() < give_up) {
for (size_t off = 0; off < bytes.size() && !closed;) {
auto n = strm->write(bytes.data() + off,
(std::min)(chunk, bytes.size() - off));
if (n <= 0) { break; }
off += static_cast<size_t>(n);
auto next = std::chrono::steady_clock::now() + gap;
while (!closed && std::chrono::steady_clock::now() < next) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
}
while (!closed) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
EXPECT_LT(close_took, std::chrono::milliseconds(2500));
}
// Empty masked Ping frames, enough to keep the socket full
static std::string ping_flood() {
const std::string ping("\x89\x80\0\0\0\0", 6);
std::string bytes;
while (bytes.size() < 64 * 1024) {
bytes += ping;
}
return bytes;
}
// A masked 20-byte Ping; sent a byte at a time, it outlasts the close timeout
static std::string ping_with_payload() {
return std::string("\x89\x8e\0\0\0\0", 6) + std::string(14, 'x');
}
TEST(WebSocketCloseTest, BoundedAgainstPeerThatKeepsSending) {
expect_close_bounded(/*use_tls=*/false, ping_flood(), ping_flood().size(),
std::chrono::milliseconds(0));
}
TEST(WebSocketCloseTest, BoundedAgainstPeerThatTricklesAFrame) {
expect_close_bounded(/*use_tls=*/false, ping_with_payload(), 1,
std::chrono::milliseconds(300));
}
#ifdef CPPHTTPLIB_SSL_ENABLED
TEST(WebSocketCloseTest, SSLBoundedAgainstPeerThatKeepsSending) {
expect_close_bounded(/*use_tls=*/true, ping_flood(), ping_flood().size(),
std::chrono::milliseconds(0));
}
TEST(WebSocketCloseTest, SSLBoundedAgainstPeerThatTricklesAFrame) {
expect_close_bounded(/*use_tls=*/true, ping_with_payload(), 1,
std::chrono::milliseconds(300));
}
#endif