mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-18 19:40:57 +00:00
Compare commits
12
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7800c3dc5b | ||
|
|
baba35b8d3 | ||
|
|
92435b31b3 | ||
|
|
5a956da084 | ||
|
|
cd446a4e01 | ||
|
|
eb364f1167 | ||
|
|
748dbfade6 | ||
|
|
d7b0d4bedb | ||
|
|
4af42e1907 | ||
|
|
1b40ce850b | ||
|
|
f57672b390 | ||
|
|
654fd3546c |
No files matched your search
+1
-1
@@ -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()`.
|
||||
|
||||
**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.
|
||||
|
||||
|
||||
@@ -1172,7 +1172,7 @@ cli.set_default_headers({
|
||||
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
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ langs = ["en", "ja"]
|
||||
|
||||
[site]
|
||||
title = "cpp-httplib"
|
||||
version = "0.66.0"
|
||||
version = "0.67.0"
|
||||
hostname = "https://yhirose.github.io"
|
||||
base_path = "/cpp-httplib"
|
||||
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.
|
||||
|
||||
> **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()`が戻るまでには、処理中のリクエストが終わるのを待つ時間がかかります。タイムアウトを強制したい場合は、シャットダウン用のタイマーを別途用意するなど、アプリケーション側の工夫が必要です。
|
||||
|
||||
> **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`を返すので、ループ側で扱う必要があります。
|
||||
@@ -8,8 +8,8 @@
|
||||
#ifndef CPPHTTPLIB_HTTPLIB_H
|
||||
#define CPPHTTPLIB_HTTPLIB_H
|
||||
|
||||
#define CPPHTTPLIB_VERSION "0.66.0"
|
||||
#define CPPHTTPLIB_VERSION_NUM "0x004200"
|
||||
#define CPPHTTPLIB_VERSION "0.67.0"
|
||||
#define CPPHTTPLIB_VERSION_NUM "0x004300"
|
||||
|
||||
#ifdef _WIN32
|
||||
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00
|
||||
@@ -2443,6 +2443,8 @@ protected:
|
||||
}
|
||||
|
||||
std::atomic<socket_t> svr_sock_{INVALID_SOCKET};
|
||||
// stop() takes the listening socket away; listen() then waits for handlers
|
||||
bool is_stopping() const { return svr_sock_ == INVALID_SOCKET; }
|
||||
|
||||
std::vector<std::string> trusted_proxies_;
|
||||
|
||||
@@ -3023,18 +3025,25 @@ protected:
|
||||
// shutdown_socket
|
||||
// close_socket
|
||||
// disconnect
|
||||
// should ONLY be called when socket_mutex_ is locked, and only when
|
||||
// no other thread is using the socket.
|
||||
// should ONLY be called on socket_ with socket_mutex_ locked, or on a
|
||||
// connection being opened, and only when no other thread is using it.
|
||||
virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully);
|
||||
void shutdown_socket(Socket &socket) const;
|
||||
void close_socket(Socket &socket);
|
||||
void disconnect(bool gracefully);
|
||||
|
||||
// Under socket_mutex_: whether the connection kept alive can take another
|
||||
// request. One the peer has closed, or one set up under TLS settings changed
|
||||
// since, is closed instead.
|
||||
// request. One with anything to read, or one set up under TLS settings
|
||||
// changed since, is closed instead.
|
||||
bool socket_reusable();
|
||||
|
||||
// Makes socket_ ready for a request, with socket_mutex_ held by guard. A new
|
||||
// connection is opened without the lock, so that stop() can end it.
|
||||
bool
|
||||
prepare_socket(std::unique_lock<std::mutex> &guard,
|
||||
std::chrono::time_point<std::chrono::steady_clock> start_time,
|
||||
Response &res, bool &success, Error &error);
|
||||
|
||||
// TLS settings apply as a connection is set up, so a change makes the
|
||||
// connection kept alive unfit for the next request.
|
||||
void tls_settings_changed() { tls_settings_version_++; }
|
||||
@@ -3060,16 +3069,21 @@ protected:
|
||||
// How many send() calls are running on this client, guarded by
|
||||
// request_mutex_
|
||||
size_t send_depth_ = 0;
|
||||
// Also guarded by request_mutex_
|
||||
size_t socket_tls_settings_version_ = 0;
|
||||
|
||||
// These are all protected under socket_mutex
|
||||
size_t socket_requests_in_flight_ = 0;
|
||||
std::thread::id socket_requests_are_from_thread_ = std::thread::id();
|
||||
bool socket_should_be_closed_when_request_is_done_ = false;
|
||||
size_t socket_tls_settings_version_ = 0;
|
||||
|
||||
// Set by stop() while a request is in flight. The request's reads poll it,
|
||||
// because shutting the socket down does not wake a read on Windows.
|
||||
// Set by stop(), cleared as a send() or an open_stream() starts. Its waits
|
||||
// poll it: shutdown() does not reach a connection being opened, nor wake a
|
||||
// read on Windows.
|
||||
std::atomic<bool> stop_requested_{false};
|
||||
std::function<bool()> stop_interrupt() const {
|
||||
return [this] { return stop_requested_.load(); };
|
||||
}
|
||||
|
||||
std::atomic<size_t> tls_settings_version_{0};
|
||||
|
||||
@@ -3866,15 +3880,14 @@ bool process_client_socket(
|
||||
std::chrono::time_point<std::chrono::steady_clock> start_time,
|
||||
std::function<bool(Stream &)> callback);
|
||||
|
||||
socket_t create_client_socket(const std::string &host, const std::string &ip,
|
||||
int port, int address_family, bool tcp_nodelay,
|
||||
bool ipv6_v6only, SocketOptions socket_options,
|
||||
time_t connection_timeout_sec,
|
||||
time_t connection_timeout_usec,
|
||||
time_t read_timeout_sec, time_t read_timeout_usec,
|
||||
time_t write_timeout_sec,
|
||||
time_t write_timeout_usec,
|
||||
const std::string &intf, Error &error);
|
||||
socket_t create_client_socket(
|
||||
const std::string &host, const std::string &ip, int port,
|
||||
int address_family, bool tcp_nodelay, bool ipv6_v6only,
|
||||
SocketOptions socket_options, time_t connection_timeout_sec,
|
||||
time_t connection_timeout_usec, time_t read_timeout_sec,
|
||||
time_t read_timeout_usec, time_t write_timeout_sec,
|
||||
time_t write_timeout_usec, const std::string &intf, Error &error,
|
||||
const std::function<bool()> &interrupted = nullptr);
|
||||
|
||||
const char *get_header_value(const Headers &headers, const std::string &key,
|
||||
const char *def, size_t id);
|
||||
@@ -4721,23 +4734,33 @@ private:
|
||||
WebSocket(
|
||||
Stream &strm, const Request &req, bool is_server,
|
||||
time_t ping_interval_sec = CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND,
|
||||
int max_missed_pongs = CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS)
|
||||
int max_missed_pongs = CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS,
|
||||
std::function<bool()> server_stopping = nullptr)
|
||||
: strm_(strm), req_(req), is_server_(is_server),
|
||||
ping_interval_sec_(ping_interval_sec),
|
||||
max_missed_pongs_(max_missed_pongs) {
|
||||
max_missed_pongs_(max_missed_pongs),
|
||||
server_stopping_(std::move(server_stopping)) {
|
||||
interrupt_reads();
|
||||
start_heartbeat();
|
||||
}
|
||||
|
||||
WebSocket(
|
||||
std::unique_ptr<Stream> &&owned_strm, const Request &req, bool is_server,
|
||||
time_t ping_interval_sec = CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND,
|
||||
int max_missed_pongs = CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS)
|
||||
int max_missed_pongs = CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS,
|
||||
std::function<bool()> server_stopping = nullptr)
|
||||
: strm_(*owned_strm), owned_strm_(std::move(owned_strm)), req_(req),
|
||||
is_server_(is_server), ping_interval_sec_(ping_interval_sec),
|
||||
max_missed_pongs_(max_missed_pongs) {
|
||||
max_missed_pongs_(max_missed_pongs),
|
||||
server_stopping_(std::move(server_stopping)) {
|
||||
interrupt_reads();
|
||||
start_heartbeat();
|
||||
}
|
||||
|
||||
void interrupt_reads();
|
||||
bool server_stopping() const {
|
||||
return server_stopping_ && server_stopping_();
|
||||
}
|
||||
void start_heartbeat();
|
||||
bool send_frame(Opcode op, const char *data, size_t len, bool fin = true);
|
||||
|
||||
@@ -4751,6 +4774,8 @@ private:
|
||||
std::atomic<bool> closed_{false};
|
||||
// Set when the peer stopped answering pings, to end a read() waiting on it
|
||||
std::atomic<bool> pong_timed_out_{false};
|
||||
// A server connection's view of Server::stop(), which ends its read() too
|
||||
std::function<bool()> server_stopping_;
|
||||
// Set once the caller has bounded read() through set_read_timeout(). Until
|
||||
// then the timeout in effect is the compile-time default, and elapsing it
|
||||
// is a failure that closes the connection, not a Timeout.
|
||||
@@ -5407,9 +5432,10 @@ bool set_sni(session_t session, const char *hostname, bool verify_hostname);
|
||||
TlsError connect(session_t session);
|
||||
TlsError accept(session_t session);
|
||||
|
||||
// Handshake with timeout (blocking until timeout)
|
||||
// Handshake with timeout (blocking until timeout, or until interrupted)
|
||||
bool connect_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
|
||||
time_t timeout_usec, TlsError *err);
|
||||
time_t timeout_usec, TlsError *err,
|
||||
const std::function<bool()> &interrupted = nullptr);
|
||||
bool accept_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
|
||||
time_t timeout_usec, TlsError *err);
|
||||
|
||||
@@ -6111,9 +6137,35 @@ inline bool is_valid_path(const std::string &path) {
|
||||
|
||||
inline bool canonicalize_path(const char *path, std::string &resolved) {
|
||||
#if defined(_WIN32)
|
||||
char buf[_MAX_PATH];
|
||||
if (_fullpath(buf, path, _MAX_PATH) == nullptr) { return false; }
|
||||
resolved = buf;
|
||||
// Resolve junctions and symlinks, as realpath() does
|
||||
auto wpath = u8string_to_wstring(path);
|
||||
if (wpath.empty()) { return false; }
|
||||
|
||||
CREATEFILE2_EXTENDED_PARAMETERS params{};
|
||||
params.dwSize = sizeof(params);
|
||||
params.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS; // Needed to open a directory
|
||||
auto h = ::CreateFile2(wpath.c_str(), 0,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
OPEN_EXISTING, ¶ms);
|
||||
if (h == INVALID_HANDLE_VALUE) { return false; }
|
||||
auto guard = scope_exit([&]() { ::CloseHandle(h); });
|
||||
|
||||
std::wstring wbuf(MAX_PATH, L'\0');
|
||||
auto len = ::GetFinalPathNameByHandleW(
|
||||
h, &wbuf[0], static_cast<DWORD>(wbuf.size()), FILE_NAME_NORMALIZED);
|
||||
if (len >= wbuf.size()) {
|
||||
wbuf.resize(len);
|
||||
len = ::GetFinalPathNameByHandleW(
|
||||
h, &wbuf[0], static_cast<DWORD>(wbuf.size()), FILE_NAME_NORMALIZED);
|
||||
}
|
||||
if (len == 0 || len >= wbuf.size()) { return false; }
|
||||
|
||||
auto n = ::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast<int>(len),
|
||||
nullptr, 0, nullptr, nullptr);
|
||||
if (n <= 0) { return false; }
|
||||
resolved.assign(static_cast<size_t>(n), '\0');
|
||||
::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast<int>(len),
|
||||
&resolved[0], n, nullptr, nullptr);
|
||||
#elif defined(PATH_MAX)
|
||||
char buf[PATH_MAX];
|
||||
if (realpath(path, buf) == nullptr) { return false; }
|
||||
@@ -6846,39 +6898,41 @@ inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) {
|
||||
return select_impl(sock, POLLOUT, sec, usec);
|
||||
}
|
||||
|
||||
// The point sec and usec from now, for select_until(). A negative sec never
|
||||
// comes, as a negative timeout waits forever in select_impl().
|
||||
// The point sec and usec from now, for select_until(). A negative sec, or one
|
||||
// past what the clock reaches, never comes.
|
||||
inline std::chrono::steady_clock::time_point deadline_after(time_t sec,
|
||||
time_t usec) {
|
||||
if (sec < 0) { return (std::chrono::steady_clock::time_point::max)(); }
|
||||
return std::chrono::steady_clock::now() + std::chrono::seconds(sec) +
|
||||
std::chrono::microseconds(usec);
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto never = (std::chrono::steady_clock::time_point::max)();
|
||||
if (sec < 0 ||
|
||||
sec >= std::chrono::duration_cast<std::chrono::seconds>(never - now)
|
||||
.count()) {
|
||||
return never;
|
||||
}
|
||||
return now + std::chrono::seconds(sec) + std::chrono::microseconds(usec);
|
||||
}
|
||||
|
||||
// Waits only for the time left until deadline, so that all the waits of one
|
||||
// TLS handshake come out of a single timeout.
|
||||
inline ssize_t select_until(socket_t sock, short events,
|
||||
std::chrono::steady_clock::time_point deadline) {
|
||||
if (deadline == (std::chrono::steady_clock::time_point::max)()) {
|
||||
return select_impl(sock, events, -1, 0);
|
||||
}
|
||||
auto left = std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
deadline - std::chrono::steady_clock::now())
|
||||
.count();
|
||||
if (left <= 0) { return 0; }
|
||||
return select_impl(sock, events, static_cast<time_t>(left / 1000000),
|
||||
static_cast<time_t>(left % 1000000));
|
||||
}
|
||||
|
||||
// select_read(), but waits in slices and gives up once interrupted() returns
|
||||
// true. A local shutdown() would not wake the wait on Windows.
|
||||
// TLS handshake come out of a single timeout. With interrupted, it waits in
|
||||
// slices and gives up once interrupted() returns true, as a local shutdown()
|
||||
// would not wake the wait on Windows.
|
||||
inline ssize_t
|
||||
select_read_interruptible(socket_t sock, time_t sec, time_t usec,
|
||||
const std::function<bool()> &interrupted) {
|
||||
if (!interrupted) { return select_read(sock, sec, usec); }
|
||||
select_until(socket_t sock, short events,
|
||||
std::chrono::steady_clock::time_point deadline,
|
||||
const std::function<bool()> &interrupted = nullptr) {
|
||||
if (!interrupted) {
|
||||
if (deadline == (std::chrono::steady_clock::time_point::max)()) {
|
||||
return select_impl(sock, events, -1, 0);
|
||||
}
|
||||
auto left = std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
deadline - std::chrono::steady_clock::now())
|
||||
.count();
|
||||
if (left <= 0) { return 0; }
|
||||
return select_impl(sock, events, static_cast<time_t>(left / 1000000),
|
||||
static_cast<time_t>(left % 1000000));
|
||||
}
|
||||
|
||||
const auto slice = std::chrono::microseconds(100000);
|
||||
auto deadline = deadline_after(sec, usec);
|
||||
while (!interrupted()) {
|
||||
auto left = std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
deadline - std::chrono::steady_clock::now());
|
||||
@@ -6888,36 +6942,46 @@ select_read_interruptible(socket_t sock, time_t sec, time_t usec,
|
||||
auto wait_usec = last ? (std::max)((left.count() + 999) / 1000 * 1000,
|
||||
static_cast<decltype(left.count())>(0))
|
||||
: slice.count();
|
||||
auto ret = select_impl(sock, POLLIN, 0, static_cast<time_t>(wait_usec));
|
||||
auto ret = select_impl(sock, events, 0, static_cast<time_t>(wait_usec));
|
||||
if (ret != 0 || last) { return ret; }
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline Error wait_until_socket_is_ready(socket_t sock, time_t sec,
|
||||
time_t usec) {
|
||||
struct pollfd pfd_read;
|
||||
pfd_read.fd = sock;
|
||||
pfd_read.events = POLLIN | POLLOUT;
|
||||
pfd_read.revents = 0;
|
||||
// select_impl(), but waits in slices and gives up once interrupted() returns
|
||||
// true
|
||||
inline ssize_t select_interruptible(socket_t sock, short events, time_t sec,
|
||||
time_t usec,
|
||||
const std::function<bool()> &interrupted) {
|
||||
if (!interrupted) { return select_impl(sock, events, sec, usec); }
|
||||
return select_until(sock, events, deadline_after(sec, usec), interrupted);
|
||||
}
|
||||
|
||||
auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
|
||||
inline ssize_t
|
||||
select_read_interruptible(socket_t sock, time_t sec, time_t usec,
|
||||
const std::function<bool()> &interrupted) {
|
||||
return select_interruptible(sock, POLLIN, sec, usec, interrupted);
|
||||
}
|
||||
|
||||
auto poll_res =
|
||||
handle_EINTR([&]() { return poll_wrapper(&pfd_read, 1, timeout); });
|
||||
|
||||
if (poll_res == 0) { return Error::ConnectionTimeout; }
|
||||
|
||||
if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) {
|
||||
auto error = 0;
|
||||
socklen_t len = sizeof(error);
|
||||
auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR,
|
||||
reinterpret_cast<char *>(&error), &len);
|
||||
auto successful = res >= 0 && !error;
|
||||
return successful ? Error::Success : Error::Connection;
|
||||
// Waits for a connect in progress, which ends as Error::Canceled once
|
||||
// interrupted() returns true
|
||||
inline Error
|
||||
wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec,
|
||||
const std::function<bool()> &interrupted = nullptr) {
|
||||
auto ret =
|
||||
select_interruptible(sock, POLLIN | POLLOUT, sec, usec, interrupted);
|
||||
if (ret == 0) {
|
||||
return interrupted && interrupted() ? Error::Canceled
|
||||
: Error::ConnectionTimeout;
|
||||
}
|
||||
if (ret < 0) { return Error::Connection; }
|
||||
|
||||
return Error::Connection;
|
||||
// A failed connect reports its error here, whatever events poll() returned
|
||||
auto error = 0;
|
||||
socklen_t len = sizeof(error);
|
||||
auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR,
|
||||
reinterpret_cast<char *>(&error), &len);
|
||||
return res >= 0 && !error ? Error::Success : Error::Connection;
|
||||
}
|
||||
|
||||
inline bool is_socket_alive(socket_t sock) {
|
||||
@@ -7734,13 +7798,15 @@ inline std::string if2ip(int address_family, const std::string &ifn) {
|
||||
}
|
||||
#endif
|
||||
|
||||
inline socket_t create_client_socket(
|
||||
const std::string &host, const std::string &ip, int port,
|
||||
int address_family, bool tcp_nodelay, bool ipv6_v6only,
|
||||
SocketOptions socket_options, time_t connection_timeout_sec,
|
||||
time_t connection_timeout_usec, time_t read_timeout_sec,
|
||||
time_t read_timeout_usec, time_t write_timeout_sec,
|
||||
time_t write_timeout_usec, const std::string &intf, Error &error) {
|
||||
inline socket_t
|
||||
create_client_socket(const std::string &host, const std::string &ip, int port,
|
||||
int address_family, bool tcp_nodelay, bool ipv6_v6only,
|
||||
SocketOptions socket_options,
|
||||
time_t connection_timeout_sec,
|
||||
time_t connection_timeout_usec, time_t read_timeout_sec,
|
||||
time_t read_timeout_usec, time_t write_timeout_sec,
|
||||
time_t write_timeout_usec, const std::string &intf,
|
||||
Error &error, const std::function<bool()> &interrupted) {
|
||||
auto sock = create_socket(
|
||||
host, ip, port, address_family, 0, tcp_nodelay, ipv6_v6only,
|
||||
std::move(socket_options),
|
||||
@@ -7766,10 +7832,13 @@ inline socket_t create_client_socket(
|
||||
error = Error::Connection;
|
||||
return false;
|
||||
}
|
||||
error = wait_until_socket_is_ready(sock2, connection_timeout_sec,
|
||||
connection_timeout_usec);
|
||||
error =
|
||||
wait_until_socket_is_ready(sock2, connection_timeout_sec,
|
||||
connection_timeout_usec, interrupted);
|
||||
if (error != Error::Success) {
|
||||
if (error == Error::ConnectionTimeout) { quit = true; }
|
||||
if (error == Error::ConnectionTimeout || error == Error::Canceled) {
|
||||
quit = true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -10910,6 +10979,7 @@ public:
|
||||
socket_t socket() const override;
|
||||
time_t duration() const override;
|
||||
void set_read_timeout(time_t sec, time_t usec = 0) override;
|
||||
void set_max_timeout(time_t msec) override;
|
||||
void set_read_interrupt(std::function<bool()> interrupted) override;
|
||||
|
||||
private:
|
||||
@@ -10917,14 +10987,13 @@ private:
|
||||
|
||||
socket_t sock_;
|
||||
tls::session_t session_;
|
||||
// WebSocket::close() shortens the read timeout from the closing thread
|
||||
// while the receive thread is inside wait_readable(), so these two are read
|
||||
// and written concurrently. The write timeouts are never mutated.
|
||||
// May change while another thread is in wait_readable(); write timeouts don't
|
||||
std::atomic<time_t> read_timeout_sec_;
|
||||
std::atomic<time_t> read_timeout_usec_;
|
||||
time_t write_timeout_sec_;
|
||||
time_t write_timeout_usec_;
|
||||
const std::chrono::time_point<std::chrono::steady_clock> start_time_;
|
||||
std::atomic<time_t> max_timeout_msec_{0};
|
||||
std::atomic<std::chrono::steady_clock::rep> start_time_;
|
||||
std::function<bool()> read_interrupted_;
|
||||
};
|
||||
|
||||
@@ -11439,6 +11508,8 @@ struct ClientTlsSessionOptions {
|
||||
// certificate verification.
|
||||
bool server_hostname_verification = true;
|
||||
std::function<SSLVerifierResponse(tls::session_t)> session_verifier;
|
||||
// When set, ends the handshake as a timeout does once it returns true
|
||||
std::function<bool()> interrupted;
|
||||
// When non-null, guards session creation against concurrent use of the
|
||||
// context. A WebSocketClient is not safe to use from several threads to
|
||||
// begin with, so it passes nothing.
|
||||
@@ -11538,8 +11609,8 @@ inline bool setup_client_tls_session(
|
||||
}
|
||||
|
||||
TlsError tls_err;
|
||||
if (!connect_nonblocking(session, sock, timeout_sec, timeout_usec,
|
||||
&tls_err)) {
|
||||
if (!connect_nonblocking(session, sock, timeout_sec, timeout_usec, &tls_err,
|
||||
options.interrupted)) {
|
||||
return fail_tls(tls_err.code, tls_err.backend_code);
|
||||
}
|
||||
|
||||
@@ -13554,7 +13625,7 @@ inline WebSocketSSLStream::WebSocketSSLStream(socket_t sock,
|
||||
read_timeout_usec_(read_timeout_usec),
|
||||
write_timeout_sec_(write_timeout_sec),
|
||||
write_timeout_usec_(write_timeout_usec),
|
||||
start_time_(std::chrono::steady_clock::now()) {
|
||||
start_time_(std::chrono::steady_clock::now().time_since_epoch().count()) {
|
||||
// The receive and send paths run on different threads, so each TLS call is
|
||||
// driven in non-blocking mode and readiness is awaited with select()
|
||||
// outside the session lock. Set the socket non-blocking once here; it is
|
||||
@@ -13573,7 +13644,18 @@ inline bool WebSocketSSLStream::is_readable() const {
|
||||
}
|
||||
|
||||
inline bool WebSocketSSLStream::wait_readable() const {
|
||||
return select_read_interruptible(sock_, read_timeout_sec_, read_timeout_usec_,
|
||||
auto max_timeout_msec = max_timeout_msec_.load();
|
||||
if (max_timeout_msec <= 0) {
|
||||
return select_read_interruptible(sock_, read_timeout_sec_,
|
||||
read_timeout_usec_, read_interrupted_) > 0;
|
||||
}
|
||||
|
||||
time_t read_timeout_sec;
|
||||
time_t read_timeout_usec;
|
||||
calc_actual_timeout(max_timeout_msec, duration(), read_timeout_sec_,
|
||||
read_timeout_usec_, read_timeout_sec, read_timeout_usec);
|
||||
|
||||
return select_read_interruptible(sock_, read_timeout_sec, read_timeout_usec,
|
||||
read_interrupted_) > 0;
|
||||
}
|
||||
|
||||
@@ -13663,12 +13745,20 @@ inline void WebSocketSSLStream::get_local_ip_and_port(std::string &ip,
|
||||
inline socket_t WebSocketSSLStream::socket() const { return sock_; }
|
||||
|
||||
inline time_t WebSocketSSLStream::duration() const {
|
||||
auto start = std::chrono::steady_clock::time_point(
|
||||
std::chrono::steady_clock::duration(start_time_.load()));
|
||||
return static_cast<time_t>(
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now() - start_time_)
|
||||
std::chrono::steady_clock::now() - start)
|
||||
.count());
|
||||
}
|
||||
|
||||
// Start time first: a reader that sees the new bound also sees its start
|
||||
inline void WebSocketSSLStream::set_max_timeout(time_t msec) {
|
||||
start_time_ = std::chrono::steady_clock::now().time_since_epoch().count();
|
||||
max_timeout_msec_ = msec;
|
||||
}
|
||||
|
||||
inline void WebSocketSSLStream::set_read_timeout(time_t sec, time_t usec) {
|
||||
read_timeout_sec_ = sec;
|
||||
read_timeout_usec_ = usec;
|
||||
@@ -14307,9 +14397,7 @@ inline bool
|
||||
Server::write_content_with_provider(Stream &strm, const Request &req,
|
||||
Response &res, const std::string &boundary,
|
||||
const std::string &content_type) {
|
||||
auto is_shutting_down = [this]() {
|
||||
return this->svr_sock_ == INVALID_SOCKET;
|
||||
};
|
||||
auto is_shutting_down = [this]() { return is_stopping(); };
|
||||
|
||||
if (res.content_length_ > 0) {
|
||||
if (!detail::is_partial_content(req, res)) {
|
||||
@@ -15479,6 +15567,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
||||
|
||||
connection_closed = true;
|
||||
if (websocket_upgraded) { *websocket_upgraded = true; }
|
||||
auto stopping = [this] { return is_stopping(); };
|
||||
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
if (req.ssl) {
|
||||
@@ -15492,7 +15581,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
||||
write_timeout_sec_, write_timeout_usec_));
|
||||
ws::WebSocket ws(std::move(ws_strm), req, true,
|
||||
websocket_ping_interval_sec_,
|
||||
websocket_max_missed_pongs_);
|
||||
websocket_max_missed_pongs_, stopping);
|
||||
ws_route->handler(req, ws);
|
||||
return true;
|
||||
}
|
||||
@@ -15500,7 +15589,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
||||
// Use WebSocket-specific read timeout instead of HTTP timeout
|
||||
strm.set_read_timeout(CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND, 0);
|
||||
ws::WebSocket ws(strm, req, true, websocket_ping_interval_sec_,
|
||||
websocket_max_missed_pongs_);
|
||||
websocket_max_missed_pongs_, stopping);
|
||||
ws_route->handler(req, ws);
|
||||
return true;
|
||||
}
|
||||
@@ -15698,7 +15787,8 @@ inline socket_t ClientImpl::create_client_socket(Error &error) const {
|
||||
proxy_host_, std::string(), proxy_port_, address_family_, tcp_nodelay_,
|
||||
ipv6_v6only_, socket_options_, connection_timeout_sec_,
|
||||
connection_timeout_usec_, read_timeout_sec_, read_timeout_usec_,
|
||||
write_timeout_sec_, write_timeout_usec_, interface_, error);
|
||||
write_timeout_sec_, write_timeout_usec_, interface_, error,
|
||||
stop_interrupt());
|
||||
}
|
||||
|
||||
// Check is custom IP or hostname specified for host_
|
||||
@@ -15710,7 +15800,7 @@ inline socket_t ClientImpl::create_client_socket(Error &error) const {
|
||||
connect_host, ip, port_, address_family_, tcp_nodelay_, ipv6_v6only_,
|
||||
socket_options_, connection_timeout_sec_, connection_timeout_usec_,
|
||||
read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
|
||||
write_timeout_usec_, interface_, error);
|
||||
write_timeout_usec_, interface_, error, stop_interrupt());
|
||||
}
|
||||
|
||||
inline bool ClientImpl::create_and_connect_socket(Socket &socket,
|
||||
@@ -15775,14 +15865,8 @@ inline void ClientImpl::disconnect(bool gracefully) {
|
||||
inline bool ClientImpl::socket_reusable() {
|
||||
if (!socket_.is_open()) { return false; }
|
||||
|
||||
auto is_alive = detail::is_socket_alive(socket_.sock);
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
if (is_alive && is_ssl() && tls::is_peer_closed(socket_.ssl, socket_.sock)) {
|
||||
is_alive = false;
|
||||
}
|
||||
#endif
|
||||
if (!is_alive) {
|
||||
// Peer seems gone — non-graceful shutdown to avoid SIGPIPE.
|
||||
// RFC 9112 9.2: data with no request outstanding is not a response
|
||||
if (detail::select_read(socket_.sock, 0, 0) != 0) {
|
||||
disconnect(/*gracefully=*/false);
|
||||
return false;
|
||||
}
|
||||
@@ -15794,6 +15878,33 @@ inline bool ClientImpl::socket_reusable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool ClientImpl::prepare_socket(
|
||||
std::unique_lock<std::mutex> &guard,
|
||||
std::chrono::time_point<std::chrono::steady_clock> start_time,
|
||||
Response &res, bool &success, Error &error) {
|
||||
auto opened = true;
|
||||
if (!socket_reusable()) {
|
||||
Socket socket;
|
||||
guard.unlock();
|
||||
opened = ensure_socket_connection(socket, error);
|
||||
if (opened) {
|
||||
opened = setup_proxy_connection(socket, start_time, res, success, error);
|
||||
} else {
|
||||
success = false;
|
||||
}
|
||||
guard.lock();
|
||||
socket_ = socket;
|
||||
}
|
||||
|
||||
if (stop_requested_) {
|
||||
disconnect(/*gracefully=*/false);
|
||||
error = Error::Canceled;
|
||||
success = false;
|
||||
return false;
|
||||
}
|
||||
return opened;
|
||||
}
|
||||
|
||||
inline bool ClientImpl::read_response_line(Stream &strm, const Request &req,
|
||||
Response &res,
|
||||
bool skip_100_continue) const {
|
||||
@@ -15838,7 +15949,10 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) {
|
||||
// the call the caller made reports a failure, once, with its request.
|
||||
auto outermost = send_depth_++ == 0;
|
||||
auto se = detail::scope_exit([&] { send_depth_--; });
|
||||
if (outermost) { error = Error::Success; }
|
||||
if (outermost) {
|
||||
error = Error::Success;
|
||||
stop_requested_ = false;
|
||||
}
|
||||
|
||||
auto ret = send_(req, res, error);
|
||||
if (error == Error::SSLPeerCouldBeClosed_) {
|
||||
@@ -15864,26 +15978,16 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) {
|
||||
#endif
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(socket_mutex_);
|
||||
std::unique_lock<std::mutex> guard(socket_mutex_);
|
||||
|
||||
// Set this to false immediately - if it ever gets set to true by the end
|
||||
// of the request, we know another thread instructed us to close the
|
||||
// socket.
|
||||
socket_should_be_closed_when_request_is_done_ = false;
|
||||
|
||||
// A redirect or a retry sent inside a request keeps its stop()
|
||||
if (socket_requests_in_flight_ == 0) { stop_requested_ = false; }
|
||||
|
||||
if (!socket_reusable()) {
|
||||
if (!ensure_socket_connection(socket_, error)) { return false; }
|
||||
|
||||
{
|
||||
auto success = true;
|
||||
if (!setup_proxy_connection(socket_, req.start_time_, res, success,
|
||||
error)) {
|
||||
return success;
|
||||
}
|
||||
}
|
||||
auto success = true;
|
||||
if (!prepare_socket(guard, req.start_time_, res, success, error)) {
|
||||
return success;
|
||||
}
|
||||
|
||||
// Mark the current socket as being in use so that it cannot be closed by
|
||||
@@ -15921,7 +16025,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) {
|
||||
});
|
||||
|
||||
ret = process_socket(socket_, req.start_time_, [&](Stream &strm) {
|
||||
strm.set_read_interrupt([this] { return stop_requested_.load(); });
|
||||
strm.set_read_interrupt(stop_interrupt());
|
||||
return handle_request(strm, req, res, close_connection, error);
|
||||
});
|
||||
|
||||
@@ -16036,23 +16140,16 @@ inline ClientImpl::StreamHandle ClientImpl::open_stream_(Request &req) {
|
||||
handle.connection_ = detail::make_unique<ClientConnection>();
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(socket_mutex_);
|
||||
// A request in flight uses socket_ without holding socket_mutex_
|
||||
std::lock_guard<std::recursive_mutex> request_mutex_guard(request_mutex_);
|
||||
if (send_depth_ == 0) { stop_requested_ = false; }
|
||||
std::unique_lock<std::mutex> guard(socket_mutex_);
|
||||
|
||||
if (!socket_reusable()) {
|
||||
if (!ensure_socket_connection(socket_, handle.error)) {
|
||||
handle.response.reset();
|
||||
return handle;
|
||||
}
|
||||
|
||||
{
|
||||
auto success = true;
|
||||
auto start_time = std::chrono::steady_clock::now();
|
||||
if (!setup_proxy_connection(socket_, start_time, *handle.response,
|
||||
success, handle.error)) {
|
||||
if (!success) { handle.response.reset(); }
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
auto success = true;
|
||||
if (!prepare_socket(guard, std::chrono::steady_clock::now(),
|
||||
*handle.response, success, handle.error)) {
|
||||
if (!success) { handle.response.reset(); }
|
||||
return handle;
|
||||
}
|
||||
|
||||
transfer_socket_ownership_to_handle(handle);
|
||||
@@ -16078,6 +16175,11 @@ inline ClientImpl::StreamHandle ClientImpl::open_stream_(Request &req) {
|
||||
|
||||
auto &strm = *handle.stream_;
|
||||
|
||||
// Only for the head, as the handle may outlive this client
|
||||
strm.set_read_interrupt(stop_interrupt());
|
||||
auto release_interrupt =
|
||||
detail::scope_exit([&] { strm.set_read_interrupt(nullptr); });
|
||||
|
||||
// After connecting, as for a buffered request
|
||||
add_credential_headers(req);
|
||||
|
||||
@@ -16431,9 +16533,10 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req,
|
||||
// to do these things to the socket if another thread is using the socket.
|
||||
std::lock_guard<std::mutex> guard(socket_mutex_);
|
||||
// Also when the request asked for it (e.g. a body that was never sent,
|
||||
// or "Connection: close"), so that a redirect or an auth retry below does
|
||||
// not reuse the socket.
|
||||
// or "Connection: close") or bytes follow the response (RFC 9112 9.2), so
|
||||
// that a redirect or an auth retry below does not reuse the socket.
|
||||
if (socket_should_be_closed_when_request_is_done_ || close_connection ||
|
||||
strm.is_readable() ||
|
||||
detail::has_header_token(res.headers, "Connection", "close") ||
|
||||
(res.version == "HTTP/1.0" && res.reason != "Connection established")) {
|
||||
disconnect(/*gracefully=*/true);
|
||||
@@ -16939,6 +17042,11 @@ ClientImpl::send_with_content_provider_and_receiver(
|
||||
|
||||
if (enc.second) { req.set_header("Content-Encoding", enc.second); }
|
||||
|
||||
// A compressed or chunked body is framed by the library, not the caller
|
||||
if (enc.second || content_provider_without_length) {
|
||||
req.headers.erase("Content-Length");
|
||||
}
|
||||
|
||||
if (enc.first && !content_provider_without_length) {
|
||||
auto &compressor = enc.first;
|
||||
|
||||
@@ -18146,6 +18254,8 @@ inline Result ClientImpl::Options(const std::string &path,
|
||||
inline void ClientImpl::stop() {
|
||||
std::lock_guard<std::mutex> guard(socket_mutex_);
|
||||
|
||||
stop_requested_ = true;
|
||||
|
||||
// If there is anything ongoing right now, the ONLY thread-safe thing we can
|
||||
// do is to shutdown_socket, so that threads using this socket suddenly
|
||||
// discover they can't read/write any more and error out. Everything else
|
||||
@@ -18153,9 +18263,6 @@ inline void ClientImpl::stop() {
|
||||
// are not thread-safe.
|
||||
if (socket_requests_in_flight_ > 0) {
|
||||
shutdown_socket(socket_);
|
||||
// shutdown() does not wake a read waiting in WSAPoll on Windows, so the
|
||||
// request's reads also give up once they see this flag.
|
||||
stop_requested_ = true;
|
||||
|
||||
// Aside from that, we set a flag for the socket to be closed when we're
|
||||
// done.
|
||||
@@ -19137,8 +19244,12 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
|
||||
auto cleanup = detail::scope_exit([&] {
|
||||
if (handshake_done) { shutdown(session, !websocket_upgraded && ret); }
|
||||
free_session(session);
|
||||
detail::shutdown_socket(sock);
|
||||
detail::close_socket(sock);
|
||||
if (handshake_done) {
|
||||
detail::drain_and_close_socket(sock);
|
||||
} else {
|
||||
detail::shutdown_socket(sock);
|
||||
detail::close_socket(sock);
|
||||
}
|
||||
});
|
||||
|
||||
// Perform TLS accept handshake with timeout
|
||||
@@ -19299,6 +19410,8 @@ inline bool SSLClient::setup_proxy_connection(
|
||||
}
|
||||
|
||||
if (!initialize_ssl(socket, error)) {
|
||||
shutdown_socket(socket);
|
||||
close_socket(socket);
|
||||
success = false;
|
||||
return false;
|
||||
}
|
||||
@@ -19306,8 +19419,7 @@ inline bool SSLClient::setup_proxy_connection(
|
||||
return true;
|
||||
}
|
||||
|
||||
// Assumes that socket_mutex_ is locked and that there are no requests in
|
||||
// flight
|
||||
// Assumes that socket is a connection being opened, which no other thread uses
|
||||
inline bool SSLClient::connect_with_proxy(
|
||||
Socket &socket,
|
||||
std::chrono::time_point<std::chrono::steady_clock> start_time,
|
||||
@@ -19322,6 +19434,7 @@ inline bool SSLClient::connect_with_proxy(
|
||||
socket.sock, read_timeout_sec_, read_timeout_usec_,
|
||||
write_timeout_sec_, write_timeout_usec_, max_timeout_msec_,
|
||||
start_time, [&](Stream &strm) {
|
||||
strm.set_read_interrupt(stop_interrupt());
|
||||
Request req2;
|
||||
req2.method = "CONNECT";
|
||||
req2.path = connect_target;
|
||||
@@ -19362,6 +19475,7 @@ inline bool SSLClient::connect_with_proxy(
|
||||
socket.sock, read_timeout_sec_, read_timeout_usec_,
|
||||
write_timeout_sec_, write_timeout_usec_, max_timeout_msec_,
|
||||
start_time, [&](Stream &strm) {
|
||||
strm.set_read_interrupt(stop_interrupt());
|
||||
Request req3;
|
||||
req3.method = "CONNECT";
|
||||
req3.path = connect_target;
|
||||
@@ -19556,6 +19670,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) {
|
||||
detail::ClientTlsSessionOptions options;
|
||||
options.server_hostname_verification = server_hostname_verification_;
|
||||
options.session_verifier = session_verifier_;
|
||||
options.interrupted = stop_interrupt();
|
||||
options.ctx_mutex = &ctx_mutex_;
|
||||
#ifdef CPPHTTPLIB_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
|
||||
// Skip Schannel when a custom CA cert is specified, as the Windows
|
||||
@@ -20502,7 +20617,8 @@ inline TlsError accept(session_t session) {
|
||||
|
||||
inline bool connect_nonblocking(session_t session, socket_t sock,
|
||||
time_t timeout_sec, time_t timeout_usec,
|
||||
TlsError *err) {
|
||||
TlsError *err,
|
||||
const std::function<bool()> &interrupted) {
|
||||
if (!session) {
|
||||
if (err) { err->code = ErrorCode::Fatal; }
|
||||
return false;
|
||||
@@ -20527,10 +20643,14 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
|
||||
auto ssl_err = SSL_get_error(ssl, res);
|
||||
switch (ssl_err) {
|
||||
case SSL_ERROR_WANT_READ:
|
||||
if (detail::select_until(sock, POLLIN, deadline) > 0) { continue; }
|
||||
if (detail::select_until(sock, POLLIN, deadline, interrupted) > 0) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
if (detail::select_until(sock, POLLOUT, deadline) > 0) { continue; }
|
||||
if (detail::select_until(sock, POLLOUT, deadline, interrupted) > 0) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
@@ -21857,7 +21977,8 @@ inline TlsError accept(session_t session) {
|
||||
|
||||
inline bool connect_nonblocking(session_t session, socket_t sock,
|
||||
time_t timeout_sec, time_t timeout_usec,
|
||||
TlsError *err) {
|
||||
TlsError *err,
|
||||
const std::function<bool()> &interrupted) {
|
||||
if (!session) {
|
||||
if (err) { err->code = ErrorCode::Fatal; }
|
||||
return false;
|
||||
@@ -21876,9 +21997,13 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
|
||||
// Non-fatal TLS 1.3 ticket; retry immediately.
|
||||
if (impl::mbedtls_is_session_ticket(ret)) { continue; }
|
||||
if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
|
||||
if (detail::select_until(sock, POLLIN, deadline) > 0) { continue; }
|
||||
if (detail::select_until(sock, POLLIN, deadline, interrupted) > 0) {
|
||||
continue;
|
||||
}
|
||||
} else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
if (detail::select_until(sock, POLLOUT, deadline) > 0) { continue; }
|
||||
if (detail::select_until(sock, POLLOUT, deadline, interrupted) > 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// TlsError or timeout
|
||||
@@ -23030,7 +23155,8 @@ inline TlsError accept(session_t session) {
|
||||
|
||||
inline bool connect_nonblocking(session_t session, socket_t sock,
|
||||
time_t timeout_sec, time_t timeout_usec,
|
||||
TlsError *err) {
|
||||
TlsError *err,
|
||||
const std::function<bool()> &interrupted) {
|
||||
if (!session) {
|
||||
if (err) { err->code = ErrorCode::Fatal; }
|
||||
return false;
|
||||
@@ -23048,9 +23174,13 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
|
||||
while ((ret = wolfSSL_connect(wsession->ssl)) != SSL_SUCCESS) {
|
||||
int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
|
||||
if (ssl_error == SSL_ERROR_WANT_READ) {
|
||||
if (detail::select_until(sock, POLLIN, deadline) > 0) { continue; }
|
||||
if (detail::select_until(sock, POLLIN, deadline, interrupted) > 0) {
|
||||
continue;
|
||||
}
|
||||
} else if (ssl_error == SSL_ERROR_WANT_WRITE) {
|
||||
if (detail::select_until(sock, POLLOUT, deadline) > 0) { continue; }
|
||||
if (detail::select_until(sock, POLLOUT, deadline, interrupted) > 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Error or timeout
|
||||
@@ -23715,6 +23845,13 @@ inline ReadResult WebSocket::read(std::string &msg) {
|
||||
};
|
||||
|
||||
std::unique_lock<std::mutex> read_lock(read_mutex_);
|
||||
// A server going down closes the connection rather than wait on it: its
|
||||
// listen() waits for this handler to return.
|
||||
auto going_away = [&]() -> ReadResult {
|
||||
read_lock.unlock();
|
||||
close(CloseStatus::GoingAway, "server stopping");
|
||||
return Fail;
|
||||
};
|
||||
while (!closed_) {
|
||||
Opcode opcode;
|
||||
std::string payload;
|
||||
@@ -23723,6 +23860,7 @@ inline ReadResult WebSocket::read(std::string &msg) {
|
||||
impl::FrameRead r =
|
||||
impl::read_websocket_frame(strm_, opcode, payload, fin, is_server_,
|
||||
CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH);
|
||||
if (r != impl::FrameRead::Ok && server_stopping()) { return going_away(); }
|
||||
// A timeout landed on a frame boundary of an open connection: it is
|
||||
// untouched and still usable, so hand control back without closing it.
|
||||
// That is only useful to a caller who asked for the timeout; the
|
||||
@@ -23746,6 +23884,10 @@ inline ReadResult WebSocket::read(std::string &msg) {
|
||||
// Handle fragmentation
|
||||
if (!fin) {
|
||||
while (true) {
|
||||
// A close() from another thread ends the message being assembled,
|
||||
// as it ends the loop over frames above: a peer that keeps sending
|
||||
// fragments never lets a read reach a wait
|
||||
if (closed_) { return Fail; }
|
||||
Opcode cont_opcode;
|
||||
std::string cont_payload;
|
||||
bool cont_fin;
|
||||
@@ -23764,6 +23906,7 @@ inline ReadResult WebSocket::read(std::string &msg) {
|
||||
if (impl::read_websocket_frame(strm_, cont_opcode, cont_payload,
|
||||
cont_fin, is_server_,
|
||||
max_len) != impl::FrameRead::Ok) {
|
||||
if (server_stopping()) { return going_away(); }
|
||||
closed_ = true;
|
||||
return Fail;
|
||||
}
|
||||
@@ -23841,14 +23984,19 @@ inline void WebSocket::close(CloseStatus status, const std::string &reason) {
|
||||
std::unique_lock<std::mutex> read_lock(read_mutex_, std::try_to_lock);
|
||||
if (!read_lock.owns_lock()) { return; }
|
||||
|
||||
// Use a short timeout to avoid hanging if the peer doesn't respond.
|
||||
// A max timeout turns a negative (unbounded) read timeout into 0
|
||||
strm_.set_read_timeout(CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND, 0);
|
||||
strm_.set_max_timeout(CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND * 1000);
|
||||
// The max timeout cuts a trickled frame short, the deadline a frame flood
|
||||
auto deadline =
|
||||
detail::deadline_after(CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND, 0);
|
||||
|
||||
Opcode op;
|
||||
std::string resp;
|
||||
bool fin;
|
||||
while (impl::read_websocket_frame(strm_, op, resp, fin, is_server_, 125) ==
|
||||
impl::FrameRead::Ok) {
|
||||
while (std::chrono::steady_clock::now() < deadline &&
|
||||
impl::read_websocket_frame(strm_, op, resp, fin, is_server_, 125) ==
|
||||
impl::FrameRead::Ok) {
|
||||
if (op == Opcode::Close) { break; }
|
||||
}
|
||||
}
|
||||
@@ -23864,12 +24012,17 @@ inline WebSocket::~WebSocket() {
|
||||
strm_.set_read_interrupt(nullptr);
|
||||
}
|
||||
|
||||
// A pong timeout ends a read() blocked on the silent peer, and so does the
|
||||
// server stopping: its listen() waits for every handler to return.
|
||||
inline void WebSocket::interrupt_reads() {
|
||||
auto watches_pongs = ping_interval_sec_ > 0 && max_missed_pongs_ > 0;
|
||||
if (!watches_pongs && !server_stopping_) { return; }
|
||||
strm_.set_read_interrupt(
|
||||
[this] { return pong_timed_out_.load() || server_stopping(); });
|
||||
}
|
||||
|
||||
inline void WebSocket::start_heartbeat() {
|
||||
if (ping_interval_sec_ <= 0) { return; }
|
||||
// A pong timeout ends a read() blocked on the silent peer
|
||||
if (max_missed_pongs_ > 0) {
|
||||
strm_.set_read_interrupt([this] { return pong_timed_out_.load(); });
|
||||
}
|
||||
ping_thread_ = std::thread([this]() {
|
||||
std::unique_lock<std::mutex> lock(ping_mutex_);
|
||||
// The predicate keeps a spurious wakeup from sending a ping early
|
||||
|
||||
+2
-7
@@ -37,11 +37,6 @@ file(
|
||||
)
|
||||
|
||||
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
|
||||
NAMES openssl
|
||||
PATHS ${OPENSSL_INCLUDE_DIR}/../bin
|
||||
@@ -61,7 +56,7 @@ if(HTTPLIB_IS_USING_OPENSSL)
|
||||
COMMAND_ERROR_IS_FATAL ANY
|
||||
)
|
||||
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}
|
||||
COMMAND_ERROR_IS_FATAL ANY
|
||||
)
|
||||
@@ -72,7 +67,7 @@ if(HTTPLIB_IS_USING_OPENSSL)
|
||||
COMMAND_ERROR_IS_FATAL ANY
|
||||
)
|
||||
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
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
COMMAND_ERROR_IS_FATAL ANY
|
||||
|
||||
+1
-1
@@ -260,7 +260,7 @@ endif
|
||||
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)
|
||||
|
||||
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)
|
||||
@file $@
|
||||
|
||||
|
||||
+5
-7
@@ -1,15 +1,9 @@
|
||||
#!/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 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 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 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
|
||||
@@ -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
|
||||
# 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
|
||||
|
||||
# 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
|
||||
+731
@@ -3600,6 +3600,197 @@ TEST(ClientStopTest, StopEndsWaitForResponse) {
|
||||
EXPECT_EQ(Error::Read, res.error());
|
||||
}
|
||||
|
||||
// A listener whose backlog completes the TCP connections of clients, and that
|
||||
// never answers them
|
||||
struct SilentListener {
|
||||
socket_t sock = INVALID_SOCKET;
|
||||
sockaddr_in addr{};
|
||||
int port = 0;
|
||||
|
||||
explicit SilentListener(int backlog = 1) {
|
||||
sock = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||
addr.sin_family = AF_INET;
|
||||
::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
|
||||
socklen_t len = sizeof(addr);
|
||||
if (::bind(sock, reinterpret_cast<sockaddr *>(&addr), len) != 0 ||
|
||||
::listen(sock, backlog) != 0 ||
|
||||
::getsockname(sock, reinterpret_cast<sockaddr *>(&addr), &len) != 0) {
|
||||
detail::close_socket(sock);
|
||||
sock = INVALID_SOCKET;
|
||||
}
|
||||
port = ntohs(addr.sin_port);
|
||||
}
|
||||
~SilentListener() { detail::close_socket(sock); }
|
||||
|
||||
// The next connection, whose reads give up after 5 seconds
|
||||
socket_t accept() const {
|
||||
auto peer = ::accept(sock, nullptr, nullptr);
|
||||
if (peer != INVALID_SOCKET) {
|
||||
detail::set_socket_opt_time(peer, SOL_SOCKET, SO_RCVTIMEO, 5, 0);
|
||||
}
|
||||
return peer;
|
||||
}
|
||||
};
|
||||
|
||||
static bool receives_a_byte(socket_t peer) {
|
||||
char c = 0;
|
||||
return detail::read_socket(peer, &c, 1, 0) == 1;
|
||||
}
|
||||
|
||||
// Runs op, stops cli once the listener receives the first byte op sends, and
|
||||
// expects op to end with error soon after
|
||||
template <typename ClientType, typename Op>
|
||||
static void expect_stop_ends(ClientType &cli, const SilentListener &listener,
|
||||
Op op, Error error = Error::Canceled) {
|
||||
auto ended = std::async(std::launch::async, op);
|
||||
auto peer = listener.accept();
|
||||
auto close_peer = detail::scope_exit([&] { detail::close_socket(peer); });
|
||||
ASSERT_NE(INVALID_SOCKET, peer);
|
||||
ASSERT_TRUE(receives_a_byte(peer));
|
||||
|
||||
cli.stop();
|
||||
EXPECT_EQ(std::future_status::ready, ended.wait_for(std::chrono::seconds(2)));
|
||||
EXPECT_EQ(error, ended.get());
|
||||
}
|
||||
|
||||
// Stops itself from another thread as the connection opens, the last moment a
|
||||
// stop() cancels it. It waits for stop() at most a second, so a stop() that
|
||||
// waits for the connection fails the test instead of hanging it.
|
||||
class StoppedAsConnectedClient : public ClientImpl {
|
||||
public:
|
||||
using ClientImpl::ClientImpl;
|
||||
|
||||
protected:
|
||||
bool ensure_socket_connection(Socket &socket, Error &error) override {
|
||||
if (!ClientImpl::ensure_socket_connection(socket, error)) { return false; }
|
||||
stopper_ = std::async(std::launch::async, [this] { stop(); });
|
||||
stopper_.wait_for(std::chrono::seconds(1));
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
std::future<void> stopper_;
|
||||
};
|
||||
|
||||
TEST(ClientStopTest, StopAsTheConnectionOpensEndsTheRequest) {
|
||||
SilentListener listener;
|
||||
ASSERT_NE(INVALID_SOCKET, listener.sock);
|
||||
|
||||
StoppedAsConnectedClient cli("127.0.0.1", listener.port);
|
||||
cli.set_read_timeout(std::chrono::seconds(3));
|
||||
EXPECT_EQ(Error::Canceled, cli.Get("/").error());
|
||||
auto peer = listener.accept();
|
||||
EXPECT_FALSE(receives_a_byte(peer));
|
||||
detail::close_socket(peer);
|
||||
|
||||
auto handle = cli.open_stream("GET", "/");
|
||||
EXPECT_FALSE(handle.is_valid());
|
||||
EXPECT_EQ(Error::Canceled, handle.error);
|
||||
peer = listener.accept();
|
||||
EXPECT_FALSE(receives_a_byte(peer));
|
||||
detail::close_socket(peer);
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
// A connect that a listener with a full backlog never completes
|
||||
TEST(ClientStopTest, StopEndsAConnect) {
|
||||
SilentListener listener(0);
|
||||
ASSERT_NE(INVALID_SOCKET, listener.sock);
|
||||
std::vector<socket_t> backlog;
|
||||
auto close_backlog = detail::scope_exit([&] {
|
||||
for (auto sock : backlog) {
|
||||
detail::close_socket(sock);
|
||||
}
|
||||
});
|
||||
for (int i = 0; i < 8; i++) {
|
||||
auto sock = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
||||
::connect(sock, reinterpret_cast<const sockaddr *>(&listener.addr),
|
||||
sizeof(listener.addr));
|
||||
backlog.push_back(sock);
|
||||
}
|
||||
|
||||
Client cli("127.0.0.1", listener.port);
|
||||
cli.set_connection_timeout(std::chrono::seconds(3));
|
||||
auto ended =
|
||||
std::async(std::launch::async, [&] { return cli.Get("/").error(); });
|
||||
|
||||
// A stop() before the request starts does not apply to it, so it repeats
|
||||
auto status = std::future_status::timeout;
|
||||
for (auto i = 0; i < 200 && status != std::future_status::ready; i++) {
|
||||
cli.stop();
|
||||
status = ended.wait_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
EXPECT_EQ(std::future_status::ready, status);
|
||||
EXPECT_EQ(Error::Canceled, ended.get());
|
||||
}
|
||||
#endif
|
||||
|
||||
// The stream holds the socket while it waits for the head, out of the reach
|
||||
// of shutdown()
|
||||
TEST(ClientStopTest, StopEndsTheWaitForTheHeadOfAStream) {
|
||||
SilentListener listener;
|
||||
ASSERT_NE(INVALID_SOCKET, listener.sock);
|
||||
|
||||
Client cli("127.0.0.1", listener.port);
|
||||
cli.set_read_timeout(std::chrono::seconds(3));
|
||||
expect_stop_ends(
|
||||
cli, listener, [&] { return cli.open_stream("GET", "/").error; },
|
||||
Error::Read);
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
// A server that never answers the ClientHello
|
||||
TEST(ClientStopTest, StopEndsATlsHandshake) {
|
||||
SilentListener listener;
|
||||
ASSERT_NE(INVALID_SOCKET, listener.sock);
|
||||
|
||||
SSLClient cli("127.0.0.1", listener.port);
|
||||
cli.enable_server_certificate_verification(false);
|
||||
cli.set_connection_timeout(std::chrono::seconds(3));
|
||||
expect_stop_ends(cli, listener, [&] { return cli.Get("/").error(); });
|
||||
expect_stop_ends(cli, listener,
|
||||
[&] { return cli.open_stream("GET", "/").error; });
|
||||
}
|
||||
|
||||
// A proxy that takes the CONNECT and never answers it
|
||||
TEST(ClientStopTest, StopEndsAProxyTunnel) {
|
||||
SilentListener listener;
|
||||
ASSERT_NE(INVALID_SOCKET, listener.sock);
|
||||
|
||||
SSLClient cli("example.com", 443);
|
||||
cli.set_proxy("127.0.0.1", listener.port);
|
||||
cli.set_read_timeout(std::chrono::seconds(3));
|
||||
expect_stop_ends(cli, listener, [&] { return cli.Get("/").error(); });
|
||||
}
|
||||
#endif
|
||||
|
||||
// A read timeout past what the clock reaches waits for the response
|
||||
TEST(ClientReadTimeoutTest, PastTheClockWaitsForTheResponse) {
|
||||
SilentListener listener;
|
||||
ASSERT_NE(INVALID_SOCKET, listener.sock);
|
||||
|
||||
Client cli("127.0.0.1", listener.port);
|
||||
cli.set_read_timeout(
|
||||
static_cast<time_t>((std::min)(static_cast<long long>(
|
||||
(std::numeric_limits<time_t>::max)()),
|
||||
10000000000LL)),
|
||||
0);
|
||||
auto res = std::async(std::launch::async, [&] { return cli.Get("/"); });
|
||||
|
||||
auto peer = listener.accept();
|
||||
auto close_peer = detail::scope_exit([&] { detail::close_socket(peer); });
|
||||
ASSERT_NE(INVALID_SOCKET, peer);
|
||||
ASSERT_TRUE(receives_a_byte(peer));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
std::string answer = "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n"
|
||||
"Connection: close\r\n\r\nok";
|
||||
detail::send_socket(peer, answer.data(), answer.size(), 0);
|
||||
|
||||
auto result = res.get();
|
||||
ASSERT_TRUE(result);
|
||||
EXPECT_EQ("ok", result->body);
|
||||
}
|
||||
|
||||
static std::string remove_whitespace(const std::string &input) {
|
||||
std::string output;
|
||||
output.reserve(input.size());
|
||||
@@ -13415,6 +13606,175 @@ TEST(KeepAliveTest, MaxCountZeroSetsNoLimit) {
|
||||
EXPECT_EQ(1u, remote_ports.size());
|
||||
}
|
||||
|
||||
// Forward declaration (see the base64_encode note).
|
||||
namespace httplib {
|
||||
namespace detail {
|
||||
ssize_t select_read(socket_t sock, time_t sec, time_t usec);
|
||||
} // namespace detail
|
||||
} // namespace httplib
|
||||
|
||||
// RFC 9112 9.2: an unsolicited 408 must not be read as the next response
|
||||
static void expect_no_reuse_with_unread_data(bool use_tls, bool same_write) {
|
||||
#ifndef _WIN32
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
auto srv = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||
ASSERT_NE(INVALID_SOCKET, srv);
|
||||
auto srv_se = detail::scope_exit([&] { detail::close_socket(srv); });
|
||||
default_socket_options(srv);
|
||||
|
||||
sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = 0;
|
||||
::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
|
||||
ASSERT_EQ(0, ::bind(srv, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)));
|
||||
ASSERT_EQ(0, ::listen(srv, 2));
|
||||
socklen_t addr_len = sizeof(addr);
|
||||
ASSERT_EQ(0,
|
||||
::getsockname(srv, reinterpret_cast<sockaddr *>(&addr), &addr_len));
|
||||
auto port = ntohs(addr.sin_port);
|
||||
|
||||
struct Peer {
|
||||
socket_t sock = INVALID_SOCKET;
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
tls::session_t session = nullptr;
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
tls::ctx_t ctx = nullptr;
|
||||
if (use_tls) {
|
||||
ctx = tls::create_server_context();
|
||||
tls::set_server_cert_file(ctx, SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
|
||||
nullptr);
|
||||
}
|
||||
auto ctx_se = detail::scope_exit([&] {
|
||||
if (ctx) { tls::free_context(ctx); }
|
||||
});
|
||||
#endif
|
||||
|
||||
auto close_peer = [](Peer &peer) {
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
if (peer.session) { tls::free_session(peer.session); }
|
||||
#endif
|
||||
if (peer.sock != INVALID_SOCKET) { detail::close_socket(peer.sock); }
|
||||
};
|
||||
auto accept_peer = [&]() {
|
||||
Peer peer;
|
||||
if (detail::select_read(srv, 5, 0) <= 0) { return peer; }
|
||||
peer.sock = ::accept(srv, nullptr, nullptr);
|
||||
if (peer.sock == INVALID_SOCKET) { return peer; }
|
||||
detail::set_socket_opt_time(peer.sock, SOL_SOCKET, SO_RCVTIMEO, 5, 0);
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
if (use_tls) {
|
||||
peer.session = tls::create_session(ctx, peer.sock);
|
||||
tls::TlsError err;
|
||||
if (!tls::accept_nonblocking(peer.session, peer.sock, 5, 0, &err)) {
|
||||
close_peer(peer);
|
||||
return Peer();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return peer;
|
||||
};
|
||||
auto read_some = [](const Peer &peer, char *buf, size_t len) -> ssize_t {
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
tls::TlsError err;
|
||||
if (peer.session) { return tls::read(peer.session, buf, len, err); }
|
||||
#endif
|
||||
return detail::read_socket(peer.sock, buf, len, 0);
|
||||
};
|
||||
auto read_head = [&](const Peer &peer) {
|
||||
std::string head;
|
||||
char buf[1024];
|
||||
while (head.find("\r\n\r\n") == std::string::npos) {
|
||||
auto n = read_some(peer, buf, sizeof(buf));
|
||||
if (n <= 0) { break; }
|
||||
head.append(buf, static_cast<size_t>(n));
|
||||
}
|
||||
};
|
||||
auto send_text = [](const Peer &peer, const std::string &text) {
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
tls::TlsError err;
|
||||
if (peer.session) {
|
||||
tls::write(peer.session, text.data(), text.size(), err);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
detail::send_socket(peer.sock, text.data(), text.size(), 0);
|
||||
};
|
||||
|
||||
const std::string first = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nfirst";
|
||||
const std::string stray = "HTTP/1.1 408 Request Timeout\r\n"
|
||||
"Connection: close\r\nContent-Length: 0\r\n\r\n";
|
||||
|
||||
// Sent once the first response is read, so that it lands on an idle socket
|
||||
std::promise<void> first_response_read;
|
||||
auto first_response_read_future = first_response_read.get_future();
|
||||
auto server_thread = std::thread([&] {
|
||||
auto peer1 = accept_peer();
|
||||
if (peer1.sock == INVALID_SOCKET) { return; }
|
||||
read_head(peer1);
|
||||
if (same_write) {
|
||||
send_text(peer1, first + stray);
|
||||
} else {
|
||||
send_text(peer1, first);
|
||||
first_response_read_future.wait_for(std::chrono::seconds(5));
|
||||
send_text(peer1, stray);
|
||||
}
|
||||
|
||||
// peer1 stays open, so only the unread data can tell the client
|
||||
auto peer2 = accept_peer();
|
||||
if (peer2.sock != INVALID_SOCKET) {
|
||||
read_head(peer2);
|
||||
send_text(peer2, "HTTP/1.1 200 OK\r\nContent-Length: 6\r\n"
|
||||
"Connection: close\r\n\r\nsecond");
|
||||
close_peer(peer2);
|
||||
}
|
||||
close_peer(peer1);
|
||||
});
|
||||
auto server_se = detail::scope_exit([&] { server_thread.join(); });
|
||||
|
||||
Client cli((use_tls ? "https://127.0.0.1:" : "http://127.0.0.1:") +
|
||||
std::to_string(port));
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
cli.enable_server_certificate_verification(false);
|
||||
#endif
|
||||
cli.set_keep_alive(true);
|
||||
cli.set_read_timeout(5, 0);
|
||||
|
||||
auto res = cli.Get("/1");
|
||||
first_response_read.set_value();
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ("first", res->body);
|
||||
|
||||
if (!same_write) { ASSERT_GT(detail::select_read(cli.socket(), 5, 0), 0); }
|
||||
|
||||
res = cli.Get("/2");
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
EXPECT_EQ("second", res->body);
|
||||
}
|
||||
|
||||
TEST(KeepAliveTest, ClientDoesNotReuseConnectionWithUnreadData) {
|
||||
expect_no_reuse_with_unread_data(/*use_tls=*/false, /*same_write=*/false);
|
||||
}
|
||||
|
||||
TEST(KeepAliveTest, ClientDoesNotReuseConnectionWithDataPastResponse) {
|
||||
expect_no_reuse_with_unread_data(/*use_tls=*/false, /*same_write=*/true);
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
TEST(KeepAliveTest, SSLClientDoesNotReuseConnectionWithUnreadData) {
|
||||
expect_no_reuse_with_unread_data(/*use_tls=*/true, /*same_write=*/false);
|
||||
}
|
||||
|
||||
TEST(KeepAliveTest, SSLClientDoesNotReuseConnectionWithDataPastResponse) {
|
||||
expect_no_reuse_with_unread_data(/*use_tls=*/true, /*same_write=*/true);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(KeepAliveTest, Issue1959) {
|
||||
Server svr;
|
||||
svr.set_keep_alive_timeout(5);
|
||||
@@ -13945,8 +14305,95 @@ TEST(DataSinkTest, CompressedRequestProviderThatFinishesEarlyFails) {
|
||||
ASSERT_FALSE(res);
|
||||
EXPECT_EQ(Error::Write, res.error());
|
||||
}
|
||||
|
||||
TEST(ClientRequestFramingTest, CompressedBodyReplacesCallerContentLength) {
|
||||
Server svr;
|
||||
|
||||
std::string received;
|
||||
size_t content_length_count = 0;
|
||||
bool has_transfer_encoding = true;
|
||||
svr.Post("/echo", [&](const Request &req, Response &res) {
|
||||
received = req.body;
|
||||
content_length_count = req.get_header_value_count("Content-Length");
|
||||
has_transfer_encoding = req.has_header("Transfer-Encoding");
|
||||
res.set_content("ok", "text/plain");
|
||||
});
|
||||
svr.Get("/next", [](const Request &, Response &res) {
|
||||
res.set_content("next", "text/plain");
|
||||
});
|
||||
|
||||
auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
listen_thread.join();
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
});
|
||||
|
||||
svr.wait_until_ready();
|
||||
|
||||
Client cli(HOST, PORT);
|
||||
cli.set_keep_alive(true);
|
||||
cli.set_compress(true);
|
||||
|
||||
const std::string body = "hello world";
|
||||
Headers headers = {{"Content-Length", std::to_string(body.size())}};
|
||||
auto res = cli.Post("/echo", headers, body, "text/plain");
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
EXPECT_EQ(body, received);
|
||||
EXPECT_EQ(1u, content_length_count);
|
||||
EXPECT_FALSE(has_transfer_encoding);
|
||||
|
||||
// The rest of a truncated body would be read as this request
|
||||
auto res2 = cli.Get("/next");
|
||||
ASSERT_TRUE(res2) << "Error: " << to_string(res2.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res2->status);
|
||||
EXPECT_EQ("next", res2->body);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(ClientRequestFramingTest, ChunkedProviderDropsCallerContentLength) {
|
||||
Server svr;
|
||||
|
||||
std::string received;
|
||||
size_t content_length_count = 1;
|
||||
bool has_transfer_encoding = false;
|
||||
svr.Post("/echo", [&](const Request &req, Response &res) {
|
||||
received = req.body;
|
||||
content_length_count = req.get_header_value_count("Content-Length");
|
||||
has_transfer_encoding = req.has_header("Transfer-Encoding");
|
||||
res.set_content("ok", "text/plain");
|
||||
});
|
||||
|
||||
auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
listen_thread.join();
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
});
|
||||
|
||||
svr.wait_until_ready();
|
||||
|
||||
Client cli(HOST, PORT);
|
||||
cli.set_keep_alive(true);
|
||||
|
||||
const std::string body = "hello world";
|
||||
Headers headers = {{"Content-Length", "5"}};
|
||||
auto res = cli.Post(
|
||||
"/echo", headers,
|
||||
[&](size_t /*offset*/, DataSink &sink) {
|
||||
sink.write(body.data(), body.size());
|
||||
sink.done();
|
||||
return true;
|
||||
},
|
||||
"text/plain");
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
EXPECT_EQ(body, received);
|
||||
EXPECT_EQ(0u, content_length_count);
|
||||
EXPECT_TRUE(has_transfer_encoding);
|
||||
}
|
||||
|
||||
TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) {
|
||||
Server svr;
|
||||
|
||||
@@ -24347,6 +24794,45 @@ TEST(OpenStreamPartialWriteTest, BodyIsWrittenWhole) {
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(OpenStreamConcurrencyTest, RequestInFlightKeepsItsConnection) {
|
||||
std::promise<void> slow_started;
|
||||
auto slow_started_future = slow_started.get_future();
|
||||
|
||||
Server svr;
|
||||
svr.Get("/slow", [&](const Request &, Response &res) {
|
||||
slow_started.set_value();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
res.set_content("slow", "text/plain");
|
||||
});
|
||||
svr.Get("/fast", [](const Request &, Response &res) {
|
||||
res.set_content("fast", "text/plain");
|
||||
});
|
||||
|
||||
auto port = svr.bind_to_any_port(HOST);
|
||||
std::thread t([&]() { svr.listen_after_bind(); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
t.join();
|
||||
});
|
||||
svr.wait_until_ready();
|
||||
|
||||
Client cli(HOST, port);
|
||||
|
||||
std::string slow_body;
|
||||
std::thread slow_thread([&] {
|
||||
auto res = cli.Get("/slow");
|
||||
if (res) { slow_body = res->body; }
|
||||
});
|
||||
slow_started_future.wait();
|
||||
|
||||
auto result = stream::Get(cli, "/fast");
|
||||
auto fast_body = result.is_valid() ? result.read_all() : std::string();
|
||||
slow_thread.join();
|
||||
|
||||
EXPECT_EQ("slow", slow_body);
|
||||
EXPECT_EQ("fast", fast_body);
|
||||
}
|
||||
|
||||
// Serves `response` to the single request `fn` makes with a fresh client.
|
||||
template <typename Fn>
|
||||
static void with_single_response(const std::string &response, Fn fn) {
|
||||
@@ -30054,6 +30540,59 @@ TEST(WebSocketTest, AssemblesFragmentsAroundAPing) {
|
||||
EXPECT_EQ(std::string("\x8a\x01p", 3), std::string(pong, 3));
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, CloseEndsAReadOfEndlessFragments) {
|
||||
#ifndef _WIN32
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
std::atomic<bool> read_ended{false};
|
||||
std::atomic<bool> sending{true};
|
||||
|
||||
Server svr;
|
||||
svr.set_websocket_ping_interval(0);
|
||||
svr.WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) {
|
||||
std::thread reader([&]() {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {}
|
||||
read_ended = true;
|
||||
});
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
ws.close();
|
||||
reader.join();
|
||||
});
|
||||
|
||||
auto port = svr.bind_to_any_port("127.0.0.1");
|
||||
std::thread t([&]() { svr.listen_after_bind(); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
t.join();
|
||||
});
|
||||
svr.wait_until_ready();
|
||||
|
||||
auto sock = connect_raw_websocket(port, "/ws");
|
||||
ASSERT_NE(INVALID_SOCKET, sock);
|
||||
auto se_sock = detail::scope_exit([&] { detail::close_socket(sock); });
|
||||
|
||||
// A Text message whose empty continuation frames never end
|
||||
std::thread sender([&]() {
|
||||
auto data = masked_frame(0x01, "");
|
||||
while (sending) {
|
||||
data += masked_frame(0x00, "");
|
||||
if (detail::send_socket(sock, data.data(), data.size(), 0) <= 0) {
|
||||
return;
|
||||
}
|
||||
data.clear();
|
||||
}
|
||||
});
|
||||
|
||||
for (int i = 0; i < 300 && !read_ended; i++) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
sending = false;
|
||||
sender.join();
|
||||
EXPECT_TRUE(read_ended);
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, NegativePingIntervalSendsNoPings) {
|
||||
Server svr;
|
||||
svr.set_websocket_ping_interval(-1);
|
||||
@@ -30142,6 +30681,121 @@ TEST(WebSocketTest, PongTimeoutEndsABlockedReadOverTLS) {
|
||||
}
|
||||
#endif
|
||||
|
||||
// listen() returns only after every handler has, so Server::stop() has to end
|
||||
// a WebSocket handler's read() waiting on a client that stays connected and
|
||||
// quiet, whether or not the handler bounded its reads, and the client's
|
||||
// connection ends with it.
|
||||
static void check_stop_ends_websocket_read(Server &svr, const char *scheme,
|
||||
bool bounded) {
|
||||
svr.WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) {
|
||||
if (bounded) { ws.set_read_timeout(0, 50000); }
|
||||
std::string msg;
|
||||
while (ws.read(msg) != ws::Fail) {}
|
||||
});
|
||||
|
||||
auto port = svr.bind_to_any_port(HOST);
|
||||
std::thread t([&]() { svr.listen_after_bind(); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
if (t.joinable()) {
|
||||
svr.stop();
|
||||
t.join();
|
||||
}
|
||||
});
|
||||
svr.wait_until_ready();
|
||||
|
||||
ws::WebSocketClient client(std::string(scheme) +
|
||||
"://localhost:" + std::to_string(port) + "/ws");
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
client.enable_server_certificate_verification(false);
|
||||
#endif
|
||||
client.set_read_timeout(2);
|
||||
ASSERT_TRUE(client.connect());
|
||||
|
||||
auto stopped = std::async(std::launch::async, [&] {
|
||||
svr.stop();
|
||||
t.join();
|
||||
});
|
||||
std::string msg;
|
||||
EXPECT_EQ(ws::Fail, client.read(msg));
|
||||
EXPECT_FALSE(client.is_open());
|
||||
auto in_time =
|
||||
stopped.wait_for(std::chrono::seconds(5)) == std::future_status::ready;
|
||||
// A handler the stop did not reach ends when the client leaves
|
||||
if (!in_time) { client.close(); }
|
||||
stopped.wait();
|
||||
EXPECT_TRUE(in_time);
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, StopEndsAWaitingRead) {
|
||||
Server svr;
|
||||
check_stop_ends_websocket_read(svr, "ws", false);
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, StopEndsAWaitingBoundedRead) {
|
||||
Server svr;
|
||||
check_stop_ends_websocket_read(svr, "ws", true);
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
TEST(WebSocketTest, StopEndsAWaitingReadOverTLS) {
|
||||
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
|
||||
ASSERT_TRUE(svr.is_valid());
|
||||
check_stop_ends_websocket_read(svr, "wss", false);
|
||||
}
|
||||
#endif
|
||||
|
||||
// The stop reaches the client as a Close with 1001 Going Away, not as a
|
||||
// dropped connection, whether the handler waits for a message or for the rest
|
||||
// of one: `sent` is what the client sends first.
|
||||
static void check_stop_closes_with_going_away(const std::string &sent) {
|
||||
Server svr;
|
||||
svr.set_websocket_ping_interval(0);
|
||||
svr.WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {}
|
||||
});
|
||||
|
||||
auto port = svr.bind_to_any_port("127.0.0.1");
|
||||
std::thread t([&]() { svr.listen_after_bind(); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
t.join();
|
||||
});
|
||||
svr.wait_until_ready();
|
||||
|
||||
// Closed before the join above, which a handler the stop did not reach
|
||||
// would otherwise hold up.
|
||||
auto sock = connect_raw_websocket(port, "/ws");
|
||||
ASSERT_NE(INVALID_SOCKET, sock);
|
||||
auto se_sock = detail::scope_exit([&] { detail::close_socket(sock); });
|
||||
ASSERT_EQ(static_cast<ssize_t>(sent.size()),
|
||||
detail::send_socket(sock, sent.data(), sent.size(), 0));
|
||||
// Let the handler take what was sent and wait for more
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
svr.stop();
|
||||
|
||||
// FIN + Close, its length, then the code: unmasked, from a server
|
||||
std::string head;
|
||||
char c;
|
||||
while (head.size() < 4 && ::recv(sock, &c, 1, 0) == 1) {
|
||||
head += c;
|
||||
}
|
||||
const std::string reason = "server stopping";
|
||||
EXPECT_EQ(std::string("\x88") + static_cast<char>(2 + reason.size()) +
|
||||
"\x03\xe9",
|
||||
head);
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, StopClosesWithGoingAway) {
|
||||
check_stop_closes_with_going_away("");
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, StopMidMessageClosesWithGoingAway) {
|
||||
// A Text frame without FIN: the first fragment of a message
|
||||
check_stop_closes_with_going_away(masked_frame(0x01, "half"));
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, AnswersAMalformedCloseWithProtocolError) {
|
||||
Server svr;
|
||||
svr.set_websocket_ping_interval(0);
|
||||
@@ -30712,6 +31366,49 @@ TEST(SymlinkTest, SymlinkEscapeFromBaseDirectory) {
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::Forbidden_403, res->status);
|
||||
}
|
||||
#else
|
||||
TEST(SymlinkTest, JunctionEscapeFromBaseDirectory) {
|
||||
auto secret_dir = std::string("junction_test_secret");
|
||||
auto served_dir = std::string("junction_test_served");
|
||||
auto secret_file = secret_dir + "\\secret.txt";
|
||||
auto junction_path = served_dir + "\\escape";
|
||||
|
||||
::CreateDirectoryA(secret_dir.c_str(), nullptr);
|
||||
::CreateDirectoryA(served_dir.c_str(), nullptr);
|
||||
|
||||
{
|
||||
std::ofstream ofs(secret_file);
|
||||
ofs << "SECRET_DATA";
|
||||
}
|
||||
|
||||
// A junction needs no privilege to create, unlike a symlink
|
||||
auto cmd = "mklink /J " + junction_path + " " + secret_dir + " >nul";
|
||||
ASSERT_EQ(0, std::system(cmd.c_str()));
|
||||
|
||||
auto se = detail::scope_exit([&] {
|
||||
::RemoveDirectoryA(junction_path.c_str());
|
||||
std::remove(secret_file.c_str());
|
||||
::RemoveDirectoryA(served_dir.c_str());
|
||||
::RemoveDirectoryA(secret_dir.c_str());
|
||||
});
|
||||
|
||||
Server svr;
|
||||
svr.set_mount_point("/", served_dir);
|
||||
|
||||
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
|
||||
auto se2 = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
listen_thread.join();
|
||||
});
|
||||
svr.wait_until_ready();
|
||||
|
||||
Client cli("localhost", PORT);
|
||||
|
||||
// Junction pointing outside base dir should be blocked
|
||||
auto res = cli.Get("/escape/secret.txt");
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::Forbidden_403, res->status);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Sends `outer_head` (with every "{len}" replaced by the length of an embedded
|
||||
@@ -32403,6 +33100,40 @@ TEST(ProxyTunnelTest, RedirectKeepsTunnelHostnameAddrMap) {
|
||||
EXPECT_NE(std::string::npos,
|
||||
connect_req.find("\r\nHost: " + target + "\r\n"));
|
||||
}
|
||||
|
||||
// A failed handshake inside the tunnel must not leave the tunnel for reuse
|
||||
TEST(ProxyTunnelTest, FailedHandshakeClosesTunnel) {
|
||||
std::atomic<int> origin_hits{0};
|
||||
proxy_tunnel_test::ScopedSSLServer origin;
|
||||
origin.svr().Get(".*", [&](const Request &, Response &res) {
|
||||
origin_hits++;
|
||||
res.set_content("ok", "text/plain");
|
||||
});
|
||||
origin.listen();
|
||||
|
||||
proxy_tunnel_test::ScopedConnectProxy proxy(origin.port());
|
||||
ASSERT_NE(0, proxy.port());
|
||||
|
||||
SSLClient cli("127.0.0.1", origin.port());
|
||||
cli.enable_server_certificate_verification(false);
|
||||
cli.set_proxy("127.0.0.1", proxy.port());
|
||||
cli.set_keep_alive(true);
|
||||
cli.set_session_verifier(
|
||||
[](tls::session_t) { return SSLVerifierResponse::CertificateRejected; });
|
||||
|
||||
auto res = cli.Get("/x");
|
||||
EXPECT_FALSE(res);
|
||||
ASSERT_FALSE(cli.is_socket_open());
|
||||
|
||||
res = cli.Get("/x");
|
||||
EXPECT_FALSE(res);
|
||||
EXPECT_EQ(2, proxy.connect_hits());
|
||||
|
||||
auto handle = cli.open_stream("GET", "/x");
|
||||
EXPECT_FALSE(handle.is_valid());
|
||||
EXPECT_EQ(3, proxy.connect_hits());
|
||||
EXPECT_EQ(0, origin_hits.load());
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace proxy_tunnel_test {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
default_bits = 2048
|
||||
distinguished_name = req_distinguished_name
|
||||
attributes = req_attributes
|
||||
x509_extensions = v3_ca
|
||||
prompt = no
|
||||
output_password = mypass
|
||||
|
||||
@@ -16,3 +17,8 @@ emailAddress = test@email.address
|
||||
|
||||
[req_attributes]
|
||||
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
|
||||
@@ -1,10 +1,10 @@
|
||||
// Standalone test for WebSocket automatic heartbeat.
|
||||
// Compiled with a 1-second ping interval so we can verify heartbeat behavior
|
||||
// without waiting 30 seconds.
|
||||
// Standalone test for WebSocket heartbeat and close() timing, compiled with
|
||||
// short intervals and timeouts so the tests don't wait for the defaults.
|
||||
|
||||
#define CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND 1
|
||||
#define CPPHTTPLIB_WEBSOCKET_CLIENT_READ_TIMEOUT_SECOND 3
|
||||
#define CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND 3
|
||||
#define CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND 1
|
||||
#include <httplib.h>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
@@ -289,3 +289,139 @@ TEST_F(WebSocketHeartbeatTest, MultipleHeartbeatCycles) {
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user