mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-26 20:48:24 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d85cf56d6 | ||
|
|
0fe58217ea |
No files matched your search
+1
-2
@@ -129,8 +129,7 @@ Passed to the handler registered with `Server::WebSocket()`. The handler runs in
|
|||||||
// Read next message (blocks until received, returns Fail/Text/Binary/Timeout)
|
// Read next message (blocks until received, returns Fail/Text/Binary/Timeout)
|
||||||
ReadResult read(std::string &msg);
|
ReadResult read(std::string &msg);
|
||||||
|
|
||||||
// Send messages. A send that fails (e.g. the write timeout elapsed) closes the
|
// Send messages
|
||||||
// connection, since part of the frame may already have gone out.
|
|
||||||
bool send(const std::string &data); // Text
|
bool send(const std::string &data); // Text
|
||||||
bool send(const char *data, size_t len); // Binary
|
bool send(const char *data, size_t len); // Binary
|
||||||
|
|
||||||
|
|||||||
+2
-12
@@ -4,7 +4,7 @@
|
|||||||
#
|
#
|
||||||
# Usage: ./ab.sh [--base REF] [--head REF] [--rounds N] [--duration S]
|
# Usage: ./ab.sh [--base REF] [--head REF] [--rounds N] [--duration S]
|
||||||
# [--connections N] [--threads N] [--path PATH] [--tls]
|
# [--connections N] [--threads N] [--path PATH] [--tls]
|
||||||
# [--large-mib N] [--timeout S] [--no-keep-alive]
|
# [--large-mib N] [--timeout S]
|
||||||
#
|
#
|
||||||
# --path selects the workload. The harness serves:
|
# --path selects the workload. The harness serves:
|
||||||
# / small body via set_content(); the response line, the
|
# / small body via set_content(); the response line, the
|
||||||
@@ -20,9 +20,6 @@
|
|||||||
# --tls runs the same workload over HTTPS, which writes through
|
# --tls runs the same workload over HTTPS, which writes through
|
||||||
# SSLSocketStream instead of SocketStream.
|
# SSLSocketStream instead of SocketStream.
|
||||||
#
|
#
|
||||||
# --no-keep-alive opens a new connection for every request, which measures
|
|
||||||
# the cost of accepting and setting up a connection.
|
|
||||||
#
|
|
||||||
# --timeout is bombardier's per-request timeout. Its 2s default aborts large
|
# --timeout is bombardier's per-request timeout. Its 2s default aborts large
|
||||||
# TLS responses, and the run then fails on the non-2xx check.
|
# TLS responses, and the run then fails on the non-2xx check.
|
||||||
#
|
#
|
||||||
@@ -46,7 +43,6 @@ REQ_PATH="/"
|
|||||||
TLS=0
|
TLS=0
|
||||||
LARGE_MIB=1
|
LARGE_MIB=1
|
||||||
TIMEOUT="30s"
|
TIMEOUT="30s"
|
||||||
NO_KEEP_ALIVE=0
|
|
||||||
|
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
@@ -60,16 +56,10 @@ while [ $# -gt 0 ]; do
|
|||||||
--large-mib) LARGE_MIB="$2"; shift 2 ;;
|
--large-mib) LARGE_MIB="$2"; shift 2 ;;
|
||||||
--timeout) TIMEOUT="$2"; shift 2 ;;
|
--timeout) TIMEOUT="$2"; shift 2 ;;
|
||||||
--tls) TLS=1; shift ;;
|
--tls) TLS=1; shift ;;
|
||||||
--no-keep-alive) NO_KEEP_ALIVE=1; shift ;;
|
|
||||||
*) echo "Unknown option: $1" >&2; exit 1 ;;
|
*) echo "Unknown option: $1" >&2; exit 1 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
# bombardier's default fasthttp client ignores -a, so close each connection
|
|
||||||
# with the request header instead.
|
|
||||||
CLOSE_HEADER=()
|
|
||||||
if [ "$NO_KEEP_ALIVE" -eq 1 ]; then CLOSE_HEADER=(-H "Connection: close"); fi
|
|
||||||
|
|
||||||
command -v bombardier >/dev/null || { echo "Error: bombardier not found" >&2; exit 1; }
|
command -v bombardier >/dev/null || { echo "Error: bombardier not found" >&2; exit 1; }
|
||||||
command -v python3 >/dev/null || { echo "Error: python3 not found" >&2; exit 1; }
|
command -v python3 >/dev/null || { echo "Error: python3 not found" >&2; exit 1; }
|
||||||
|
|
||||||
@@ -172,7 +162,7 @@ measure() {
|
|||||||
done
|
done
|
||||||
|
|
||||||
set +e
|
set +e
|
||||||
json=$(bombardier -c "$CONNECTIONS" -d "$DURATION" -t "$TIMEOUT" -o json -p r $INSECURE ${CLOSE_HEADER[@]+"${CLOSE_HEADER[@]}"} \
|
json=$(bombardier -c "$CONNECTIONS" -d "$DURATION" -t "$TIMEOUT" -o json -p r $INSECURE \
|
||||||
"$SCHEME://127.0.0.1:$PORT$REQ_PATH" 2>/dev/null)
|
"$SCHEME://127.0.0.1:$PORT$REQ_PATH" 2>/dev/null)
|
||||||
rc=$?
|
rc=$?
|
||||||
set -e
|
set -e
|
||||||
|
|||||||
@@ -2589,7 +2589,7 @@ private:
|
|||||||
ContentReceiver multipart_receiver) const;
|
ContentReceiver multipart_receiver) const;
|
||||||
|
|
||||||
virtual bool process_and_close_socket(socket_t sock);
|
virtual bool process_and_close_socket(socket_t sock);
|
||||||
// The connection that serves an accepted socket, with or without parking.
|
// The connection that parking_loop() keeps for a socket it has accepted.
|
||||||
virtual std::shared_ptr<KeepAliveConnection>
|
virtual std::shared_ptr<KeepAliveConnection>
|
||||||
new_keep_alive_connection(socket_t sock);
|
new_keep_alive_connection(socket_t sock);
|
||||||
|
|
||||||
@@ -3551,6 +3551,7 @@ public:
|
|||||||
int ssl_last_error() const { return last_ssl_error_; }
|
int ssl_last_error() const { return last_ssl_error_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
bool process_and_close_socket(socket_t sock) override;
|
||||||
std::shared_ptr<KeepAliveConnection>
|
std::shared_ptr<KeepAliveConnection>
|
||||||
new_keep_alive_connection(socket_t sock) override;
|
new_keep_alive_connection(socket_t sock) override;
|
||||||
struct TlsConnection;
|
struct TlsConnection;
|
||||||
@@ -4033,8 +4034,7 @@ private:
|
|||||||
|
|
||||||
PollerWaker waker_;
|
PollerWaker waker_;
|
||||||
|
|
||||||
// fds_[0] is the waker's, which is always armed; tokens_[i] belongs to
|
// fds_[0] is the waker's, always armed; tokens_[i] belongs to fds_[i].
|
||||||
// fds_[i].
|
|
||||||
std::vector<struct pollfd> fds_;
|
std::vector<struct pollfd> fds_;
|
||||||
std::vector<Token> tokens_;
|
std::vector<Token> tokens_;
|
||||||
std::unordered_map<socket_t, size_t> index_;
|
std::unordered_map<socket_t, size_t> index_;
|
||||||
@@ -4087,6 +4087,11 @@ private:
|
|||||||
// The best poller for this platform, or nullptr when it cannot be created.
|
// The best poller for this platform, or nullptr when it cannot be created.
|
||||||
std::unique_ptr<Poller> make_poller();
|
std::unique_ptr<Poller> make_poller();
|
||||||
|
|
||||||
|
// The Poller::wait() timeout until `deadline`: -1 for time_point::max(),
|
||||||
|
// otherwise whole milliseconds, rounded up and capped at INT_MAX.
|
||||||
|
int poll_timeout_until(std::chrono::steady_clock::time_point deadline,
|
||||||
|
std::chrono::steady_clock::time_point now);
|
||||||
|
|
||||||
class BufferStream final : public Stream {
|
class BufferStream final : public Stream {
|
||||||
public:
|
public:
|
||||||
BufferStream() = default;
|
BufferStream() = default;
|
||||||
@@ -5646,8 +5651,7 @@ 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);
|
const std::function<bool()> &interrupted = nullptr);
|
||||||
bool accept_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
|
bool accept_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);
|
|
||||||
|
|
||||||
// I/O (non-blocking capable)
|
// I/O (non-blocking capable)
|
||||||
ssize_t read(session_t session, void *buf, size_t len, TlsError &err);
|
ssize_t read(session_t session, void *buf, size_t len, TlsError &err);
|
||||||
@@ -7379,7 +7383,7 @@ inline void drain_and_close_socket(socket_t sock,
|
|||||||
const auto n = read_socket(sock, buf, sizeof(buf), CPPHTTPLIB_RECV_FLAGS);
|
const auto n = read_socket(sock, buf, sizeof(buf), CPPHTTPLIB_RECV_FLAGS);
|
||||||
if (n <= 0) { break; }
|
if (n <= 0) { break; }
|
||||||
total += static_cast<size_t>(n);
|
total += static_cast<size_t>(n);
|
||||||
} while (usec_left() > 0 && total < size_t(1024u * 1024u)); // bound #2
|
} while (total < size_t(1024u * 1024u) && usec_left() > 0); // bound #2
|
||||||
|
|
||||||
shutdown_socket(sock);
|
shutdown_socket(sock);
|
||||||
close_socket(sock);
|
close_socket(sock);
|
||||||
@@ -8262,6 +8266,18 @@ inline std::unique_ptr<Poller> make_poller() {
|
|||||||
return PollPoller::create();
|
return PollPoller::create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline int poll_timeout_until(std::chrono::steady_clock::time_point deadline,
|
||||||
|
std::chrono::steady_clock::time_point now) {
|
||||||
|
using namespace std::chrono;
|
||||||
|
if (deadline == (steady_clock::time_point::max)()) { return -1; }
|
||||||
|
if (deadline <= now) { return 0; }
|
||||||
|
const auto left = deadline - now;
|
||||||
|
auto msec = duration_cast<milliseconds>(left);
|
||||||
|
if (msec < left) { msec += milliseconds(1); }
|
||||||
|
const auto cap = milliseconds((std::numeric_limits<int>::max)());
|
||||||
|
return static_cast<int>((std::min)(msec, cap).count());
|
||||||
|
}
|
||||||
|
|
||||||
inline bool bind_ip_address(socket_t sock, const std::string &host) {
|
inline bool bind_ip_address(socket_t sock, const std::string &host) {
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
struct addrinfo *result;
|
struct addrinfo *result;
|
||||||
@@ -14326,9 +14342,9 @@ WebSocketSSLStream::set_read_interrupt(std::function<bool()> interrupted) {
|
|||||||
* Group 4: Server implementation
|
* Group 4: Server implementation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// A connection served across its requests. With set_keep_alive_parking(), the
|
// A kept-alive connection that the event loop of set_keep_alive_parking()
|
||||||
// event loop owns it while it is idle, and a worker while it serves a request.
|
// owns while it is idle, and a worker owns while it serves a request. A
|
||||||
// A derived class adds the transport: the stream, and for TLS the session.
|
// derived class adds the transport: the stream, and for TLS the session.
|
||||||
struct Server::KeepAliveConnection {
|
struct Server::KeepAliveConnection {
|
||||||
KeepAliveConnection(socket_t s, Server &server) : svr(server), sock(s) {}
|
KeepAliveConnection(socket_t s, Server &server) : svr(server), sock(s) {}
|
||||||
// Every owner closes it before letting go, which the transport hooks need;
|
// Every owner closes it before letting go, which the transport hooks need;
|
||||||
@@ -14348,8 +14364,8 @@ struct Server::KeepAliveConnection {
|
|||||||
// close_notify.
|
// close_notify.
|
||||||
virtual void shut_down(bool /*graceful*/) {}
|
virtual void shut_down(bool /*graceful*/) {}
|
||||||
|
|
||||||
// On a worker, with parking once the event loop has seen the socket
|
// On a worker, once the event loop has seen the socket readable. Afterwards
|
||||||
// readable. Afterwards turn.park tells whether it may be parked again.
|
// turn.park tells whether it may be parked again.
|
||||||
bool resume() {
|
bool resume() {
|
||||||
turn = Turn();
|
turn = Turn();
|
||||||
return svr.serve_guarded([this]() {
|
return svr.serve_guarded([this]() {
|
||||||
@@ -14374,11 +14390,11 @@ struct Server::KeepAliveConnection {
|
|||||||
sock = INVALID_SOCKET;
|
sock = INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
// From the worker that served it, with what resume() returned.
|
// From the worker that served it.
|
||||||
void close_served(bool ok) {
|
void close_served(bool graceful) {
|
||||||
assert(sock != INVALID_SOCKET);
|
assert(sock != INVALID_SOCKET);
|
||||||
if (opened) {
|
if (opened) {
|
||||||
shut_down(ok && !turn.websocket_upgraded);
|
shut_down(graceful);
|
||||||
detail::drain_and_close_socket(sock);
|
detail::drain_and_close_socket(sock);
|
||||||
} else {
|
} else {
|
||||||
// It never got as far as a request, e.g. a failed TLS handshake.
|
// It never got as far as a request, e.g. a failed TLS handshake.
|
||||||
@@ -14403,10 +14419,6 @@ struct Server::KeepAliveConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool wait_for_next_request() {
|
bool wait_for_next_request() {
|
||||||
if (wait_in_place) {
|
|
||||||
return detail::keep_alive(svr.svr_sock_, sock,
|
|
||||||
svr.keep_alive_timeout_sec_);
|
|
||||||
}
|
|
||||||
// The event loop saw the socket readable just before this ran.
|
// The event loop saw the socket readable just before this ran.
|
||||||
if (turn.woken) {
|
if (turn.woken) {
|
||||||
turn.woken = false;
|
turn.woken = false;
|
||||||
@@ -14430,8 +14442,6 @@ struct Server::KeepAliveConnection {
|
|||||||
int remote_port = 0;
|
int remote_port = 0;
|
||||||
int local_port = 0;
|
int local_port = 0;
|
||||||
bool opened = false; // by resume(), once open() has succeeded
|
bool opened = false; // by resume(), once open() has succeeded
|
||||||
// Waits for the next request on the worker, as without parking.
|
|
||||||
bool wait_in_place = false;
|
|
||||||
Turn turn;
|
Turn turn;
|
||||||
size_t served = 0;
|
size_t served = 0;
|
||||||
std::string remote_addr;
|
std::string remote_addr;
|
||||||
@@ -16468,10 +16478,29 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
|||||||
inline bool Server::is_valid() const { return !has_invalid_registration_; }
|
inline bool Server::is_valid() const { return !has_invalid_registration_; }
|
||||||
|
|
||||||
inline bool Server::process_and_close_socket(socket_t sock) {
|
inline bool Server::process_and_close_socket(socket_t sock) {
|
||||||
auto conn = new_keep_alive_connection(sock);
|
std::string remote_addr;
|
||||||
conn->wait_in_place = true;
|
int remote_port = 0;
|
||||||
auto ret = conn->resume();
|
detail::get_remote_ip_and_port(sock, remote_addr, remote_port);
|
||||||
conn->close_served(ret);
|
|
||||||
|
std::string local_addr;
|
||||||
|
int local_port = 0;
|
||||||
|
detail::get_local_ip_and_port(sock, local_addr, local_port);
|
||||||
|
|
||||||
|
bool websocket_upgraded = false;
|
||||||
|
auto ret = serve_guarded([&]() {
|
||||||
|
return detail::process_server_socket(
|
||||||
|
svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_,
|
||||||
|
read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
|
||||||
|
write_timeout_usec_,
|
||||||
|
[&](Stream &strm, bool close_connection, bool &connection_closed) {
|
||||||
|
return process_request(strm, remote_addr, remote_port, local_addr,
|
||||||
|
local_port, close_connection,
|
||||||
|
connection_closed, nullptr,
|
||||||
|
&websocket_upgraded);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
detail::drain_and_close_socket(sock);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16563,16 +16592,10 @@ inline bool Server::parking_loop(TaskQueue &task_queue) {
|
|||||||
poller.arm(listen_sock, listen_token) ? never : now + check_interval;
|
poller.arm(listen_sock, listen_token) ? never : now + check_interval;
|
||||||
}
|
}
|
||||||
|
|
||||||
// At most a minute, which keeps the timeout below within an int.
|
auto until =
|
||||||
auto until = (std::min)({st.next_deadline(), listen_rearm_at, next_idle,
|
(std::min)({st.next_deadline(), listen_rearm_at, next_idle,
|
||||||
st.backlog.empty() ? never : now + check_interval,
|
st.backlog.empty() ? never : now + check_interval});
|
||||||
now + std::chrono::seconds(60)});
|
poller.wait(detail::poll_timeout_until(until, now), ready);
|
||||||
// Rounded up, so that the wait does not end just short of the deadline.
|
|
||||||
auto timeout = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
||||||
until - now + std::chrono::microseconds(999));
|
|
||||||
poller.wait(static_cast<int>((std::max)(timeout.count(),
|
|
||||||
std::chrono::milliseconds::rep(0))),
|
|
||||||
ready);
|
|
||||||
if (svr_sock_ != listen_sock) { break; }
|
if (svr_sock_ != listen_sock) { break; }
|
||||||
now = Clock::now();
|
now = Clock::now();
|
||||||
|
|
||||||
@@ -16656,7 +16679,7 @@ Server::resume_connection(std::shared_ptr<KeepAliveConnection> conn) {
|
|||||||
c.release_buffers(); // it may stay idle for long
|
c.release_buffers(); // it may stay idle for long
|
||||||
if (st.give_back(conn)) { return; }
|
if (st.give_back(conn)) { return; }
|
||||||
}
|
}
|
||||||
c.close_served(ret);
|
c.close_served(!c.turn.websocket_upgraded && ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Server::output_log(const Request &req, const Response &res) const {
|
inline void Server::output_log(const Request &req, const Response &res) const {
|
||||||
@@ -20187,9 +20210,9 @@ inline bool ssl_free_buffers_is_safe(unsigned long version) {
|
|||||||
} // namespace detail
|
} // namespace detail
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// The connection of SSLServer. The worker that first resumes it runs the
|
// A connection that keep-alive parking keeps for SSLServer. The worker that
|
||||||
// handshake, so with parking one that has not sent its ClientHello yet waits
|
// first resumes it runs the handshake, so one that has not sent its
|
||||||
// in the event loop like an idle connection.
|
// ClientHello yet waits in the event loop like an idle connection.
|
||||||
struct SSLServer::TlsConnection final : Server::KeepAliveConnection {
|
struct SSLServer::TlsConnection final : Server::KeepAliveConnection {
|
||||||
TlsConnection(socket_t s, SSLServer &server)
|
TlsConnection(socket_t s, SSLServer &server)
|
||||||
: KeepAliveConnection(s, server), ssl_svr(server) {}
|
: KeepAliveConnection(s, server), ssl_svr(server) {}
|
||||||
@@ -20265,9 +20288,8 @@ inline tls::session_t SSLServer::start_session(socket_t sock) {
|
|||||||
std::lock_guard<std::mutex> guard(ctx_mutex_);
|
std::lock_guard<std::mutex> guard(ctx_mutex_);
|
||||||
if (--handshakes_in_progress_ == 0) { ctx_cv_.notify_all(); }
|
if (--handshakes_in_progress_ == 0) { ctx_cv_.notify_all(); }
|
||||||
});
|
});
|
||||||
accepted =
|
accepted = accept_nonblocking(session, sock, read_timeout_sec_,
|
||||||
accept_nonblocking(session, sock, read_timeout_sec_, read_timeout_usec_,
|
read_timeout_usec_, &tls_err);
|
||||||
&tls_err, [this]() { return is_stopping(); });
|
|
||||||
}
|
}
|
||||||
if (!accepted) {
|
if (!accepted) {
|
||||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
@@ -20287,6 +20309,47 @@ inline tls::session_t SSLServer::start_session(socket_t sock) {
|
|||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool SSLServer::process_and_close_socket(socket_t sock) {
|
||||||
|
auto session = start_session(sock);
|
||||||
|
if (!session) {
|
||||||
|
detail::shutdown_socket(sock);
|
||||||
|
detail::close_socket(sock);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use scope_exit to ensure cleanup on all paths (including exceptions)
|
||||||
|
bool ret = false;
|
||||||
|
bool websocket_upgraded = false;
|
||||||
|
auto cleanup = detail::scope_exit([&] {
|
||||||
|
tls::shutdown(session, !websocket_upgraded && ret);
|
||||||
|
tls::free_session(session);
|
||||||
|
detail::drain_and_close_socket(sock);
|
||||||
|
});
|
||||||
|
|
||||||
|
std::string remote_addr;
|
||||||
|
int remote_port = 0;
|
||||||
|
detail::get_remote_ip_and_port(sock, remote_addr, remote_port);
|
||||||
|
|
||||||
|
std::string local_addr;
|
||||||
|
int local_port = 0;
|
||||||
|
detail::get_local_ip_and_port(sock, local_addr, local_port);
|
||||||
|
|
||||||
|
ret = serve_guarded([&]() {
|
||||||
|
return detail::process_server_socket_ssl(
|
||||||
|
svr_sock_, session, sock, keep_alive_max_count_,
|
||||||
|
keep_alive_timeout_sec_, read_timeout_sec_, read_timeout_usec_,
|
||||||
|
write_timeout_sec_, write_timeout_usec_,
|
||||||
|
[&](Stream &strm, bool close_connection, bool &connection_closed) {
|
||||||
|
return process_request(
|
||||||
|
strm, remote_addr, remote_port, local_addr, local_port,
|
||||||
|
close_connection, connection_closed,
|
||||||
|
[&](Request &req) { req.ssl = session; }, &websocket_upgraded);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
inline bool SSLServer::update_certs_pem(const char *cert_pem,
|
inline bool SSLServer::update_certs_pem(const char *cert_pem,
|
||||||
const char *key_pem,
|
const char *key_pem,
|
||||||
const char *client_ca_pem,
|
const char *client_ca_pem,
|
||||||
@@ -21641,8 +21704,7 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
|
|||||||
|
|
||||||
inline bool accept_nonblocking(session_t session, socket_t sock,
|
inline bool accept_nonblocking(session_t session, socket_t sock,
|
||||||
time_t timeout_sec, time_t timeout_usec,
|
time_t timeout_sec, time_t timeout_usec,
|
||||||
TlsError *err,
|
TlsError *err) {
|
||||||
const std::function<bool()> &interrupted) {
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
if (err) { err->code = ErrorCode::Fatal; }
|
if (err) { err->code = ErrorCode::Fatal; }
|
||||||
return false;
|
return false;
|
||||||
@@ -21667,14 +21729,10 @@ inline bool accept_nonblocking(session_t session, socket_t sock,
|
|||||||
auto ssl_err = SSL_get_error(ssl, res);
|
auto ssl_err = SSL_get_error(ssl, res);
|
||||||
switch (ssl_err) {
|
switch (ssl_err) {
|
||||||
case SSL_ERROR_WANT_READ:
|
case SSL_ERROR_WANT_READ:
|
||||||
if (detail::select_until(sock, POLLIN, deadline, interrupted) > 0) {
|
if (detail::select_until(sock, POLLIN, deadline) > 0) { continue; }
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case SSL_ERROR_WANT_WRITE:
|
case SSL_ERROR_WANT_WRITE:
|
||||||
if (detail::select_until(sock, POLLOUT, deadline, interrupted) > 0) {
|
if (detail::select_until(sock, POLLOUT, deadline) > 0) { continue; }
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
@@ -23001,14 +23059,13 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
|
|||||||
|
|
||||||
inline bool accept_nonblocking(session_t session, socket_t sock,
|
inline bool accept_nonblocking(session_t session, socket_t sock,
|
||||||
time_t timeout_sec, time_t timeout_usec,
|
time_t timeout_sec, time_t timeout_usec,
|
||||||
TlsError *err,
|
TlsError *err) {
|
||||||
const std::function<bool()> &interrupted) {
|
|
||||||
// A failed handshake leaves its SNI behind, so start without it
|
// A failed handshake leaves its SNI behind, so start without it
|
||||||
impl::mbedpending_sni().clear();
|
impl::mbedpending_sni().clear();
|
||||||
|
|
||||||
// Same implementation as connect for Mbed TLS
|
// Same implementation as connect for Mbed TLS
|
||||||
bool result = connect_nonblocking(session, sock, timeout_sec, timeout_usec,
|
bool result =
|
||||||
err, interrupted);
|
connect_nonblocking(session, sock, timeout_sec, timeout_usec, err);
|
||||||
|
|
||||||
// After successful handshake, capture SNI from thread-local storage
|
// After successful handshake, capture SNI from thread-local storage
|
||||||
if (result && session) {
|
if (result && session) {
|
||||||
@@ -24183,8 +24240,7 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
|
|||||||
|
|
||||||
inline bool accept_nonblocking(session_t session, socket_t sock,
|
inline bool accept_nonblocking(session_t session, socket_t sock,
|
||||||
time_t timeout_sec, time_t timeout_usec,
|
time_t timeout_sec, time_t timeout_usec,
|
||||||
TlsError *err,
|
TlsError *err) {
|
||||||
const std::function<bool()> &interrupted) {
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
if (err) { err->code = ErrorCode::Fatal; }
|
if (err) { err->code = ErrorCode::Fatal; }
|
||||||
return false;
|
return false;
|
||||||
@@ -24202,13 +24258,9 @@ inline bool accept_nonblocking(session_t session, socket_t sock,
|
|||||||
while ((ret = wolfSSL_accept(wsession->ssl)) != SSL_SUCCESS) {
|
while ((ret = wolfSSL_accept(wsession->ssl)) != SSL_SUCCESS) {
|
||||||
int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
|
int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
|
||||||
if (ssl_error == SSL_ERROR_WANT_READ) {
|
if (ssl_error == SSL_ERROR_WANT_READ) {
|
||||||
if (detail::select_until(sock, POLLIN, deadline, interrupted) > 0) {
|
if (detail::select_until(sock, POLLIN, deadline) > 0) { continue; }
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else if (ssl_error == SSL_ERROR_WANT_WRITE) {
|
} else if (ssl_error == SSL_ERROR_WANT_WRITE) {
|
||||||
if (detail::select_until(sock, POLLOUT, deadline, interrupted) > 0) {
|
if (detail::select_until(sock, POLLOUT, deadline) > 0) { continue; }
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error or timeout
|
// Error or timeout
|
||||||
@@ -24796,13 +24848,7 @@ inline bool WebSocket::send_frame(Opcode op, const char *data, size_t len,
|
|||||||
bool fin) {
|
bool fin) {
|
||||||
std::lock_guard<std::mutex> lock(write_mutex_);
|
std::lock_guard<std::mutex> lock(write_mutex_);
|
||||||
if (closed_) { return false; }
|
if (closed_) { return false; }
|
||||||
// A failed write may have sent part of the frame, whose header announces the
|
return detail::write_websocket_frame(strm_, op, data, len, fin, !is_server_);
|
||||||
// whole length, so the peer would read the next frames as the rest of it.
|
|
||||||
if (!detail::write_websocket_frame(strm_, op, data, len, fin, !is_server_)) {
|
|
||||||
closed_ = true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline ReadResult WebSocket::read(std::string &msg) {
|
inline ReadResult WebSocket::read(std::string &msg) {
|
||||||
@@ -24810,8 +24856,12 @@ inline ReadResult WebSocket::read(std::string &msg) {
|
|||||||
// Returns false when the connection is done.
|
// Returns false when the connection is done.
|
||||||
auto handle_control = [&](Opcode op, const std::string &payload) -> bool {
|
auto handle_control = [&](Opcode op, const std::string &payload) -> bool {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case Opcode::Ping:
|
case Opcode::Ping: {
|
||||||
return send_frame(Opcode::Pong, payload.data(), payload.size());
|
std::lock_guard<std::mutex> lock(write_mutex_);
|
||||||
|
detail::write_websocket_frame(strm_, Opcode::Pong, payload.data(),
|
||||||
|
payload.size(), true, !is_server_);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
case Opcode::Pong: {
|
case Opcode::Pong: {
|
||||||
std::lock_guard<std::mutex> lock(ping_mutex_);
|
std::lock_guard<std::mutex> lock(ping_mutex_);
|
||||||
unacked_pings_ = 0;
|
unacked_pings_ = 0;
|
||||||
@@ -25010,7 +25060,11 @@ inline void WebSocket::start_heartbeat() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
if (!send_frame(Opcode::Ping, nullptr, 0)) { return; }
|
if (!send_frame(Opcode::Ping, nullptr, 0)) {
|
||||||
|
lock.lock();
|
||||||
|
closed_ = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
lock.lock();
|
lock.lock();
|
||||||
unacked_pings_++;
|
unacked_pings_++;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-5
@@ -26,11 +26,7 @@ endif()
|
|||||||
|
|
||||||
find_package(CURL REQUIRED)
|
find_package(CURL REQUIRED)
|
||||||
|
|
||||||
# include_httplib.cc checks the header-only build, as the Makefile's split
|
add_executable(httplib-test test.cc include_httplib.cc $<$<BOOL:${WIN32}>:include_windows_h.cc>)
|
||||||
# targets leave it out too.
|
|
||||||
add_executable(httplib-test test.cc
|
|
||||||
$<$<NOT:$<BOOL:${HTTPLIB_IS_COMPILED}>>:include_httplib.cc>
|
|
||||||
$<$<BOOL:${WIN32}>:include_windows_h.cc>)
|
|
||||||
target_compile_options(httplib-test PRIVATE "$<$<CXX_COMPILER_ID:MSVC>:/utf-8;/bigobj>")
|
target_compile_options(httplib-test PRIVATE "$<$<CXX_COMPILER_ID:MSVC>:/utf-8;/bigobj>")
|
||||||
target_link_libraries(httplib-test PRIVATE httplib GTest::gtest_main CURL::libcurl)
|
target_link_libraries(httplib-test PRIVATE httplib GTest::gtest_main CURL::libcurl)
|
||||||
gtest_discover_tests(httplib-test)
|
gtest_discover_tests(httplib-test)
|
||||||
|
|||||||
+3
-16
@@ -1,18 +1,5 @@
|
|||||||
// Includes httplib.h in a separate compilation unit, thus verifying that
|
// The sole purpose of this file is to include httplib.h in a separate
|
||||||
// inline keywords have not been forgotten when linked together with test.cc.
|
// compilation unit, thus verifying that inline keywords have not been forgotten
|
||||||
|
// when linked together with test.cc.
|
||||||
|
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
|
|
||||||
// Nothing in the library calls these, but subclasses that override
|
|
||||||
// process_and_close_socket() do. Instantiating them here, where the split build
|
|
||||||
// does not reach, keeps a change to process_server_socket_core() from breaking
|
|
||||||
// them unnoticed.
|
|
||||||
struct IncludeHttplibServeNothing {
|
|
||||||
bool operator()(httplib::Stream &, bool, bool &) const { return false; }
|
|
||||||
};
|
|
||||||
auto include_httplib_process_server_socket =
|
|
||||||
&httplib::detail::process_server_socket<IncludeHttplibServeNothing>;
|
|
||||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
|
||||||
auto include_httplib_process_server_socket_ssl =
|
|
||||||
&httplib::detail::process_server_socket_ssl<IncludeHttplibServeNothing>;
|
|
||||||
#endif
|
|
||||||
+70
-261
@@ -84,8 +84,7 @@ 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);
|
const std::function<bool()> &interrupted);
|
||||||
bool accept_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
|
bool accept_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);
|
|
||||||
ssize_t read(session_t session, void *buf, size_t len, TlsError &err);
|
ssize_t read(session_t session, void *buf, size_t len, TlsError &err);
|
||||||
ssize_t write(session_t session, const void *buf, size_t len, TlsError &err);
|
ssize_t write(session_t session, const void *buf, size_t len, TlsError &err);
|
||||||
void shutdown(session_t session, bool graceful);
|
void shutdown(session_t session, bool graceful);
|
||||||
@@ -157,31 +156,6 @@ static void read_file(const std::string &path, std::string &out) {
|
|||||||
fs.read(&out[0], static_cast<std::streamsize>(size));
|
fs.read(&out[0], static_cast<std::streamsize>(size));
|
||||||
}
|
}
|
||||||
|
|
||||||
// httpbingo.org intermittently answers with gateway errors unrelated to the
|
|
||||||
// client under test.
|
|
||||||
static bool is_gateway_error(const Result &res) {
|
|
||||||
return res && (res->status == StatusCode::BadGateway_502 ||
|
|
||||||
res->status == StatusCode::ServiceUnavailable_503 ||
|
|
||||||
res->status == StatusCode::GatewayTimeout_504);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename F> static Result retry_gateway_errors(F &&send) {
|
|
||||||
auto res = send();
|
|
||||||
for (auto wait : {1, 2}) {
|
|
||||||
if (!is_gateway_error(res)) { break; }
|
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(wait));
|
|
||||||
res = send();
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define SKIP_ON_GATEWAY_ERROR(res) \
|
|
||||||
do { \
|
|
||||||
if (is_gateway_error(res)) { \
|
|
||||||
GTEST_SKIP() << "gateway error " << (res)->status; \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
class UnixSocketTest : public ::testing::Test {
|
class UnixSocketTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
void TearDown() override { std::remove(pathname_.c_str()); }
|
void TearDown() override { std::remove(pathname_.c_str()); }
|
||||||
@@ -2853,8 +2827,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
|||||||
cli.set_connection_timeout(5);
|
cli.set_connection_timeout(5);
|
||||||
|
|
||||||
{
|
{
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
@@ -2862,8 +2835,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
Headers headers = {make_range_header({{1, -1}})};
|
Headers headers = {make_range_header({{1, -1}})};
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); });
|
auto res = cli.Get(path, headers);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
||||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
@@ -2871,8 +2843,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
Headers headers = {make_range_header({{1, 10}})};
|
Headers headers = {make_range_header({{1, 10}})};
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); });
|
auto res = cli.Get(path, headers);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ("bcdefghijk", res->body);
|
EXPECT_EQ("bcdefghijk", res->body);
|
||||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
@@ -2883,8 +2854,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
|||||||
// acceptable per RFC 9110 §15.3.7, so we accept either status code.
|
// acceptable per RFC 9110 §15.3.7, so we accept either status code.
|
||||||
{
|
{
|
||||||
Headers headers = {make_range_header({{0, 31}})};
|
Headers headers = {make_range_header({{0, 31}})};
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); });
|
auto res = cli.Get(path, headers);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
||||||
EXPECT_TRUE(res->status == StatusCode::OK_200 ||
|
EXPECT_TRUE(res->status == StatusCode::OK_200 ||
|
||||||
@@ -2893,8 +2863,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
Headers headers = {make_range_header({{0, -1}})};
|
Headers headers = {make_range_header({{0, -1}})};
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); });
|
auto res = cli.Get(path, headers);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
||||||
EXPECT_TRUE(res->status == StatusCode::OK_200 ||
|
EXPECT_TRUE(res->status == StatusCode::OK_200 ||
|
||||||
@@ -2906,8 +2875,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
|||||||
// in real servers, so we only verify the request succeeds.
|
// in real servers, so we only verify the request succeeds.
|
||||||
{
|
{
|
||||||
Headers headers = {make_range_header({{0, 32}})};
|
Headers headers = {make_range_header({{0, 32}})};
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); });
|
auto res = cli.Get(path, headers);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3222,9 +3190,7 @@ TEST(CancelTest, NoCancel_Online) {
|
|||||||
#endif
|
#endif
|
||||||
cli.set_connection_timeout(std::chrono::seconds(5));
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
||||||
|
|
||||||
auto res = retry_gateway_errors(
|
auto res = cli.Get(path, [](uint64_t, uint64_t) { return true; });
|
||||||
[&] { return cli.Get(path, [](uint64_t, uint64_t) { return true; }); });
|
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
@@ -3245,9 +3211,7 @@ TEST(CancelTest, WithCancelSmallPayload_Online) {
|
|||||||
Client cli(host, port);
|
Client cli(host, port);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto res = retry_gateway_errors(
|
auto res = cli.Get(path, [](uint64_t, uint64_t) { return false; });
|
||||||
[&] { return cli.Get(path, [](uint64_t, uint64_t) { return false; }); });
|
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
cli.set_connection_timeout(std::chrono::seconds(5));
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
||||||
ASSERT_TRUE(!res);
|
ASSERT_TRUE(!res);
|
||||||
EXPECT_EQ(Error::Canceled, res.error());
|
EXPECT_EQ(Error::Canceled, res.error());
|
||||||
@@ -3266,12 +3230,9 @@ TEST(CancelTest, WithCancelLargePayload_Online) {
|
|||||||
#endif
|
#endif
|
||||||
cli.set_connection_timeout(std::chrono::seconds(5));
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
||||||
|
|
||||||
auto res = retry_gateway_errors([&] {
|
uint32_t count = 0;
|
||||||
auto count = 0;
|
auto res =
|
||||||
return cli.Get(path,
|
cli.Get(path, [&count](uint64_t, uint64_t) { return (count++ == 0); });
|
||||||
[&count](uint64_t, uint64_t) { return (count++ == 0); });
|
|
||||||
});
|
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(!res);
|
ASSERT_TRUE(!res);
|
||||||
EXPECT_EQ(Error::Canceled, res.error());
|
EXPECT_EQ(Error::Canceled, res.error());
|
||||||
}
|
}
|
||||||
@@ -3861,18 +3822,14 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
{
|
{
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto res = retry_gateway_errors([&] {
|
auto res = cli.Get(
|
||||||
return cli.Get(
|
|
||||||
path, Headers{make_basic_authentication_header("hello", "world")});
|
path, Headers{make_basic_authentication_header("hello", "world")});
|
||||||
});
|
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
auto body = remove_whitespace(res->body);
|
auto body = remove_whitespace(res->body);
|
||||||
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
|
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
|
||||||
@@ -3882,8 +3839,7 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
cli.set_basic_auth("hello", "world");
|
cli.set_basic_auth("hello", "world");
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
auto body = remove_whitespace(res->body);
|
auto body = remove_whitespace(res->body);
|
||||||
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
|
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
|
||||||
@@ -3893,16 +3849,14 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
cli.set_basic_auth("hello", "bad");
|
cli.set_basic_auth("hello", "bad");
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
cli.set_basic_auth("bad", "world");
|
cli.set_basic_auth("bad", "world");
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
||||||
}
|
}
|
||||||
@@ -3921,8 +3875,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
|
|||||||
SSLClient cli(host, port);
|
SSLClient cli(host, port);
|
||||||
|
|
||||||
{
|
{
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(unauth_path); });
|
auto res = cli.Get(unauth_path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
||||||
}
|
}
|
||||||
@@ -3931,8 +3884,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
|
|||||||
|
|
||||||
cli.set_digest_auth("hello", "world");
|
cli.set_digest_auth("hello", "world");
|
||||||
for (const auto &path : paths) {
|
for (const auto &path : paths) {
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path.c_str()); });
|
auto res = cli.Get(path.c_str());
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
auto body = remove_whitespace(res->body);
|
auto body = remove_whitespace(res->body);
|
||||||
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
|
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
|
||||||
@@ -3942,8 +3894,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
|
|||||||
|
|
||||||
cli.set_digest_auth("hello", "bad");
|
cli.set_digest_auth("hello", "bad");
|
||||||
for (const auto &path : paths) {
|
for (const auto &path : paths) {
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path.c_str()); });
|
auto res = cli.Get(path.c_str());
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
||||||
}
|
}
|
||||||
@@ -4341,8 +4292,7 @@ TEST(AbsoluteRedirectTest, Redirect_Online) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
cli.set_follow_location(true);
|
cli.set_follow_location(true);
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
}
|
}
|
||||||
@@ -4358,8 +4308,7 @@ TEST(RedirectTest, Redirect_Online) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
cli.set_follow_location(true);
|
cli.set_follow_location(true);
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
}
|
}
|
||||||
@@ -4375,8 +4324,7 @@ TEST(RelativeRedirectTest, Redirect_Online) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
cli.set_follow_location(true);
|
cli.set_follow_location(true);
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
}
|
}
|
||||||
@@ -4392,8 +4340,7 @@ TEST(TooManyRedirectTest, Redirect_Online) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
cli.set_follow_location(true);
|
cli.set_follow_location(true);
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(!res);
|
ASSERT_TRUE(!res);
|
||||||
EXPECT_EQ(Error::ExceedRedirectCount, res.error());
|
EXPECT_EQ(Error::ExceedRedirectCount, res.error());
|
||||||
}
|
}
|
||||||
@@ -4420,10 +4367,8 @@ TEST(YahooRedirectTest, Redirect_Online) {
|
|||||||
TEST(HttpsToHttpRedirectTest, Redirect_Online) {
|
TEST(HttpsToHttpRedirectTest, Redirect_Online) {
|
||||||
SSLClient cli(REDIR_HOST);
|
SSLClient cli(REDIR_HOST);
|
||||||
cli.set_follow_location(true);
|
cli.set_follow_location(true);
|
||||||
auto res = retry_gateway_errors([&] {
|
auto res =
|
||||||
return cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302");
|
cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302");
|
||||||
});
|
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
}
|
}
|
||||||
@@ -4436,9 +4381,7 @@ TEST(HttpsToHttpRedirectTest2, Redirect_Online) {
|
|||||||
params.emplace("url", "http://example.com");
|
params.emplace("url", "http://example.com");
|
||||||
params.emplace("status_code", "302");
|
params.emplace("status_code", "302");
|
||||||
|
|
||||||
auto res = retry_gateway_errors(
|
auto res = cli.Get(REDIR_PATH, params, Headers{});
|
||||||
[&] { return cli.Get(REDIR_PATH, params, Headers{}); });
|
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
}
|
}
|
||||||
@@ -4450,10 +4393,7 @@ TEST(HttpsToHttpRedirectTest3, Redirect_Online) {
|
|||||||
Params params;
|
Params params;
|
||||||
params.emplace("url", "http://example.com");
|
params.emplace("url", "http://example.com");
|
||||||
|
|
||||||
auto res = retry_gateway_errors([&] {
|
auto res = cli.Get(REDIR_PATH "?status_code=302", params, Headers{});
|
||||||
return cli.Get(REDIR_PATH "?status_code=302", params, Headers{});
|
|
||||||
});
|
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
}
|
}
|
||||||
@@ -5969,40 +5909,6 @@ TEST(SSLClientServerTest, UpdateCertsPemWaitsForHandshakeInProgress) {
|
|||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(SSLClientServerTest, StopDoesNotWaitForHandshakeInProgress) {
|
|
||||||
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
|
|
||||||
ASSERT_TRUE(svr.is_valid());
|
|
||||||
// Keep-alive parking would wait for the ClientHello without a handshake.
|
|
||||||
svr.set_keep_alive_parking(false);
|
|
||||||
svr.set_read_timeout(10, 0);
|
|
||||||
|
|
||||||
thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
|
|
||||||
auto se = detail::scope_exit([&] {
|
|
||||||
svr.stop();
|
|
||||||
if (t.joinable()) { t.join(); }
|
|
||||||
});
|
|
||||||
svr.wait_until_ready();
|
|
||||||
|
|
||||||
// A connection that sends no ClientHello keeps its handshake in progress,
|
|
||||||
// once the server has had a moment to start it.
|
|
||||||
auto error = Error::Success;
|
|
||||||
auto sock = detail::create_client_socket(
|
|
||||||
HOST, "", PORT, AF_UNSPEC, false, false, nullptr,
|
|
||||||
/*connection_timeout_sec=*/5, 0,
|
|
||||||
/*read_timeout_sec=*/5, 0,
|
|
||||||
/*write_timeout_sec=*/5, 0, std::string(), error);
|
|
||||||
ASSERT_NE(INVALID_SOCKET, sock);
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
|
||||||
|
|
||||||
auto start = std::chrono::steady_clock::now();
|
|
||||||
svr.stop();
|
|
||||||
t.join();
|
|
||||||
auto elapsed = std::chrono::steady_clock::now() - start;
|
|
||||||
detail::close_socket(sock);
|
|
||||||
|
|
||||||
EXPECT_LT(elapsed, std::chrono::seconds(3));
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEST(ErrorHandlerTest, ContentLength) {
|
TEST(ErrorHandlerTest, ContentLength) {
|
||||||
@@ -6442,8 +6348,8 @@ TEST(ServerExceptionTest, ThrowingWebSocketHandlerDoesNotKillTheServer) {
|
|||||||
|
|
||||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||||
TEST(ServerExceptionTest, ThrowingContentProviderDoesNotKillTheSSLServer) {
|
TEST(ServerExceptionTest, ThrowingContentProviderDoesNotKillTheSSLServer) {
|
||||||
// SSLServer serves through its own connection type, so it is exercised on
|
// SSLServer::process_and_close_socket() is a separate overload with its own
|
||||||
// its own.
|
// guard, so it is exercised on its own.
|
||||||
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
|
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
|
||||||
ASSERT_TRUE(svr.is_valid());
|
ASSERT_TRUE(svr.is_valid());
|
||||||
svr.new_task_queue = [] { return new ThreadPool(1); };
|
svr.new_task_queue = [] { return new ThreadPool(1); };
|
||||||
@@ -13837,8 +13743,7 @@ static void expect_no_reuse_with_unread_data(bool use_tls, bool same_write) {
|
|||||||
if (use_tls) {
|
if (use_tls) {
|
||||||
peer.session = tls::create_session(ctx, peer.sock);
|
peer.session = tls::create_session(ctx, peer.sock);
|
||||||
tls::TlsError err;
|
tls::TlsError err;
|
||||||
if (!tls::accept_nonblocking(peer.session, peer.sock, 5, 0, &err,
|
if (!tls::accept_nonblocking(peer.session, peer.sock, 5, 0, &err)) {
|
||||||
nullptr)) {
|
|
||||||
close_peer(peer);
|
close_peer(peer);
|
||||||
return Peer();
|
return Peer();
|
||||||
}
|
}
|
||||||
@@ -15125,16 +15030,14 @@ TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) {
|
|||||||
cli.set_connection_timeout(5);
|
cli.set_connection_timeout(5);
|
||||||
|
|
||||||
{
|
{
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ("bcdefghijk", res->body);
|
EXPECT_EQ("bcdefghijk", res->body);
|
||||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ("bcdefghijk", res->body);
|
EXPECT_EQ("bcdefghijk", res->body);
|
||||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
@@ -16829,8 +16732,7 @@ TEST(SSLClientTest, ServerNameIndication_Online) {
|
|||||||
auto path = std::string{"/get"};
|
auto path = std::string{"/get"};
|
||||||
|
|
||||||
SSLClient cli(host, 443);
|
SSLClient cli(host, 443);
|
||||||
auto res = retry_gateway_errors([&] { return cli.Get(path); });
|
auto res = cli.Get(path);
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
ASSERT_EQ(StatusCode::OK_200, res->status);
|
ASSERT_EQ(StatusCode::OK_200, res->status);
|
||||||
}
|
}
|
||||||
@@ -18877,10 +18779,8 @@ TEST(DecodeWithChunkedEncoding, BrotliEncoding_Online) {
|
|||||||
TEST(HttpsToHttpRedirectTest, SimpleInterface_Online) {
|
TEST(HttpsToHttpRedirectTest, SimpleInterface_Online) {
|
||||||
Client cli(REDIR_HOST);
|
Client cli(REDIR_HOST);
|
||||||
cli.set_follow_location(true);
|
cli.set_follow_location(true);
|
||||||
auto res = retry_gateway_errors([&] {
|
auto res =
|
||||||
return cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302");
|
cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302");
|
||||||
});
|
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
@@ -18894,9 +18794,7 @@ TEST(HttpsToHttpRedirectTest2, SimpleInterface_Online) {
|
|||||||
params.emplace("url", "http://example.com");
|
params.emplace("url", "http://example.com");
|
||||||
params.emplace("status_code", "302");
|
params.emplace("status_code", "302");
|
||||||
|
|
||||||
auto res = retry_gateway_errors(
|
auto res = cli.Get(REDIR_PATH, params, Headers{});
|
||||||
[&] { return cli.Get(REDIR_PATH, params, Headers{}); });
|
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
}
|
}
|
||||||
@@ -18908,10 +18806,7 @@ TEST(HttpsToHttpRedirectTest3, SimpleInterface_Online) {
|
|||||||
Params params;
|
Params params;
|
||||||
params.emplace("url", "http://example.com");
|
params.emplace("url", "http://example.com");
|
||||||
|
|
||||||
auto res = retry_gateway_errors([&] {
|
auto res = cli.Get(REDIR_PATH "?status_code=302", params, Headers{});
|
||||||
return cli.Get(REDIR_PATH "?status_code=302", params, Headers{});
|
|
||||||
});
|
|
||||||
SKIP_ON_GATEWAY_ERROR(res);
|
|
||||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
}
|
}
|
||||||
@@ -25218,7 +25113,7 @@ static void with_single_tls_response(const std::string &response,
|
|||||||
nullptr);
|
nullptr);
|
||||||
auto session = tls::create_session(ctx, cli);
|
auto session = tls::create_session(ctx, cli);
|
||||||
tls::TlsError err;
|
tls::TlsError err;
|
||||||
if (tls::accept_nonblocking(session, cli, 5, 0, &err, nullptr)) {
|
if (tls::accept_nonblocking(session, cli, 5, 0, &err)) {
|
||||||
// The request head first, so that closing the socket sends a FIN
|
// The request head first, so that closing the socket sends a FIN
|
||||||
// rather than an RST for the unread bytes.
|
// rather than an RST for the unread bytes.
|
||||||
std::string req;
|
std::string req;
|
||||||
@@ -29651,59 +29546,6 @@ TEST(WebSocketPartialWriteTest, FrameIsWrittenWhole) {
|
|||||||
|
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// A send() that fails partway leaves a frame on the wire whose header announces
|
|
||||||
// more than was sent. Nothing may follow it, or the peer reads the next frames
|
|
||||||
// as the rest of it and a later payload's bytes as a frame of their own.
|
|
||||||
TEST(WebSocketPartialWriteTest, FailedWriteClosesConnection) {
|
|
||||||
signal(SIGPIPE, SIG_IGN);
|
|
||||||
|
|
||||||
const std::string large(4 * 1024 * 1024, 'A');
|
|
||||||
std::atomic<bool> large_sent{true};
|
|
||||||
std::atomic<bool> open_after_failure{true};
|
|
||||||
std::atomic<bool> handler_done{false};
|
|
||||||
|
|
||||||
Server svr;
|
|
||||||
svr.set_write_timeout(0, 200000);
|
|
||||||
svr.set_socket_options([](socket_t sock) {
|
|
||||||
int size = 8192;
|
|
||||||
setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
|
|
||||||
reinterpret_cast<const char *>(&size), sizeof(size));
|
|
||||||
});
|
|
||||||
svr.WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) {
|
|
||||||
large_sent = ws.send(large.data(), large.size());
|
|
||||||
open_after_failure = ws.is_open();
|
|
||||||
handler_done = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
ws::WebSocketClient client("ws://" + std::string(HOST) + ":" +
|
|
||||||
std::to_string(port) + "/ws");
|
|
||||||
client.set_socket_options([](socket_t sock) {
|
|
||||||
int size = 8192;
|
|
||||||
setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
|
|
||||||
reinterpret_cast<const char *>(&size), sizeof(size));
|
|
||||||
});
|
|
||||||
ASSERT_TRUE(client.connect());
|
|
||||||
|
|
||||||
// Read nothing, so the server's write times out partway through the frame.
|
|
||||||
for (int i = 0; i < 100 && !handler_done; i++) {
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
||||||
}
|
|
||||||
ASSERT_TRUE(handler_done);
|
|
||||||
|
|
||||||
ASSERT_FALSE(large_sent);
|
|
||||||
EXPECT_FALSE(open_after_failure);
|
|
||||||
|
|
||||||
client.close();
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEST(WebSocketPreRoutingTest, RejectWithoutAuth) {
|
TEST(WebSocketPreRoutingTest, RejectWithoutAuth) {
|
||||||
@@ -34207,6 +34049,20 @@ TEST_P(PollerTest, WaitHonoursItsTimeout) {
|
|||||||
EXPECT_LT(elapsed, std::chrono::seconds(1));
|
EXPECT_LT(elapsed, std::chrono::seconds(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_P(PollerTest, NegativeTimeoutWaitsUntilReady) {
|
||||||
|
TestSocketPair sp;
|
||||||
|
ASSERT_TRUE(sp.ok());
|
||||||
|
ASSERT_TRUE(poller_->arm(sp.reader(), 3));
|
||||||
|
std::thread writer([&] {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
||||||
|
sp.make_readable();
|
||||||
|
});
|
||||||
|
|
||||||
|
auto ready = wait_for(-1);
|
||||||
|
writer.join();
|
||||||
|
EXPECT_EQ(Tokens{3}, ready);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_P(PollerTest, OnlyTheReadyDescriptorsAreDelivered) {
|
TEST_P(PollerTest, OnlyTheReadyDescriptorsAreDelivered) {
|
||||||
std::vector<std::unique_ptr<TestSocketPair>> sps;
|
std::vector<std::unique_ptr<TestSocketPair>> sps;
|
||||||
for (detail::Poller::Token i = 0; i < 8; i++) {
|
for (detail::Poller::Token i = 0; i < 8; i++) {
|
||||||
@@ -34325,6 +34181,20 @@ TEST(PollerFactoryTest, MakePollerWorks) {
|
|||||||
EXPECT_TRUE(poller->wait(1000, ready));
|
EXPECT_TRUE(poller->wait(1000, ready));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PollTimeoutTest, LastsUntilTheDeadline) {
|
||||||
|
using namespace std::chrono;
|
||||||
|
const auto now = steady_clock::now();
|
||||||
|
auto timeout = [&](steady_clock::time_point deadline) {
|
||||||
|
return detail::poll_timeout_until(deadline, now);
|
||||||
|
};
|
||||||
|
EXPECT_EQ(-1, timeout((steady_clock::time_point::max)()));
|
||||||
|
EXPECT_EQ(0, timeout(now - milliseconds(5)));
|
||||||
|
EXPECT_EQ(1, timeout(now + nanoseconds(1)));
|
||||||
|
EXPECT_EQ(1, timeout(now + milliseconds(1)));
|
||||||
|
EXPECT_EQ(2, timeout(now + microseconds(1001)));
|
||||||
|
EXPECT_EQ((std::numeric_limits<int>::max)(), timeout(now + hours(1000)));
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Keep-alive parking
|
// Keep-alive parking
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -34425,10 +34295,9 @@ bool wait_until(const std::function<bool()> &cond, int timeout_ms) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void serve_ok(Server &svr, std::function<TaskQueue *(void)> new_task_queue,
|
void serve_ok(Server &svr, std::function<TaskQueue *(void)> new_task_queue) {
|
||||||
bool parking = true) {
|
|
||||||
svr.new_task_queue = std::move(new_task_queue);
|
svr.new_task_queue = std::move(new_task_queue);
|
||||||
svr.set_keep_alive_parking(parking);
|
svr.set_keep_alive_parking(true);
|
||||||
svr.Get("/", [](const Request &, Response &res) {
|
svr.Get("/", [](const Request &, Response &res) {
|
||||||
res.set_content("ok", "text/plain");
|
res.set_content("ok", "text/plain");
|
||||||
});
|
});
|
||||||
@@ -34492,63 +34361,6 @@ private:
|
|||||||
|
|
||||||
} // namespace parking_test
|
} // namespace parking_test
|
||||||
|
|
||||||
// A connection that sends nothing is closed at the keep-alive timeout, not the
|
|
||||||
// read timeout, whether or not it is parked.
|
|
||||||
TEST(KeepAliveParkingTest, FirstRequestWaitsForTheKeepAliveTimeout) {
|
|
||||||
using namespace parking_test;
|
|
||||||
// Both modes at once, since each waits a whole second.
|
|
||||||
Server svrs[2];
|
|
||||||
std::unique_ptr<RunningServer> running[2];
|
|
||||||
Sockets socks; // after `running`, so that the sockets close first
|
|
||||||
for (auto i = 0; i < 2; i++) {
|
|
||||||
serve_ok(svrs[i], threads(1), /*parking=*/i == 0);
|
|
||||||
svrs[i].set_read_timeout(10, 0);
|
|
||||||
svrs[i].set_keep_alive_timeout(1);
|
|
||||||
running[i].reset(new RunningServer(svrs[i]));
|
|
||||||
ASSERT_NE(INVALID_SOCKET, socks.connect(running[i]->port));
|
|
||||||
}
|
|
||||||
|
|
||||||
auto early = in_ms(500);
|
|
||||||
for (size_t i = 0; i < 2; i++) {
|
|
||||||
SCOPED_TRACE(i == 0 ? "parking" : "no parking");
|
|
||||||
EXPECT_FALSE(sees_eof(socks.socks[i], early));
|
|
||||||
}
|
|
||||||
auto late = in_ms(2500);
|
|
||||||
for (size_t i = 0; i < 2; i++) {
|
|
||||||
SCOPED_TRACE(i == 0 ? "parking" : "no parking");
|
|
||||||
EXPECT_TRUE(sees_eof(socks.socks[i], late));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Without parking, a request that arrived before stop() is still served when
|
|
||||||
// its worker gets to it.
|
|
||||||
TEST(KeepAliveParkingTest, RequestBeforeStopIsServedWithoutParking) {
|
|
||||||
using namespace parking_test;
|
|
||||||
std::atomic<int> ok{0}, full{0};
|
|
||||||
Server svr;
|
|
||||||
serve_ok(svr, [&] { return new CountingTaskQueue(1, 0, ok, full); }, false);
|
|
||||||
Gate gate;
|
|
||||||
svr.Get("/gate", [&](const Request &, Response &res) {
|
|
||||||
gate.wait();
|
|
||||||
res.set_content("ok", "text/plain");
|
|
||||||
});
|
|
||||||
RunningServer running(svr);
|
|
||||||
Sockets socks;
|
|
||||||
|
|
||||||
// Hold the only worker, and queue a connection whose request has arrived.
|
|
||||||
auto held = socks.connect(running.port);
|
|
||||||
ASSERT_TRUE(send_get(held, "/gate"));
|
|
||||||
ASSERT_TRUE(wait_until([&] { return gate.entered() == 1; }, 2000));
|
|
||||||
auto waiting = socks.connect(running.port);
|
|
||||||
ASSERT_TRUE(send_get(waiting));
|
|
||||||
ASSERT_TRUE(wait_until([&] { return ok == 2; }, 2000));
|
|
||||||
|
|
||||||
svr.stop();
|
|
||||||
gate.open();
|
|
||||||
EXPECT_TRUE(receives_ok(held, 2000));
|
|
||||||
EXPECT_TRUE(receives_ok(waiting, 2000));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(KeepAliveParkingTest, IdleConnectionsDoNotHoldTheWorkers) {
|
TEST(KeepAliveParkingTest, IdleConnectionsDoNotHoldTheWorkers) {
|
||||||
using namespace parking_test;
|
using namespace parking_test;
|
||||||
Server svr;
|
Server svr;
|
||||||
@@ -34803,11 +34615,9 @@ TEST(KeepAliveParkingTest, IdleTlsConnectionsDoNotHoldTheWorkers) {
|
|||||||
|
|
||||||
TEST(KeepAliveParkingTest, FailedTlsHandshakeClosesTheConnection) {
|
TEST(KeepAliveParkingTest, FailedTlsHandshakeClosesTheConnection) {
|
||||||
using namespace parking_test;
|
using namespace parking_test;
|
||||||
for (auto parking : {true, false}) {
|
|
||||||
SCOPED_TRACE(parking ? "parking" : "no parking");
|
|
||||||
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
|
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
|
||||||
ASSERT_TRUE(svr.is_valid());
|
ASSERT_TRUE(svr.is_valid());
|
||||||
serve_ok(svr, threads(1), parking);
|
serve_ok(svr, threads(1));
|
||||||
RunningServer running(svr);
|
RunningServer running(svr);
|
||||||
Sockets socks;
|
Sockets socks;
|
||||||
|
|
||||||
@@ -34815,7 +34625,6 @@ TEST(KeepAliveParkingTest, FailedTlsHandshakeClosesTheConnection) {
|
|||||||
ASSERT_TRUE(send_get(plain));
|
ASSERT_TRUE(send_get(plain));
|
||||||
EXPECT_TRUE(sees_eof(plain, in_ms(1000)));
|
EXPECT_TRUE(sees_eof(plain, in_ms(1000)));
|
||||||
EXPECT_NE(0, svr.ssl_last_error());
|
EXPECT_NE(0, svr.ssl_last_error());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(KeepAliveParkingTest, StopClosesParkedTlsConnections) {
|
TEST(KeepAliveParkingTest, StopClosesParkedTlsConnections) {
|
||||||
|
|||||||
Reference in new issue
Block a user