mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-17 19:30:54 +00:00
Bound the wait of WebSocket::close() as a whole (#2678)
close() waited for the peer's Close reply with a read timeout that applied to each read, so a peer that kept sending other frames, or sent one frame a byte at a time, held close() for as long as it liked. Bound the wait as a whole. close() sets the stream's max timeout, which shortens each wait to what is left, and stops reading frames at the deadline, which matters when the peer keeps the socket full and no wait happens at all. WebSocketSSLStream gains set_max_timeout(), the one stream a WebSocket uses that lacked it. A read() running on another thread when close() is called still waits for the reply up to its own read timeout, as documented. The docs now say how to bound it.
This commit is contained in:
1 parent
d7b0d4bedb
commit
748dbfade6
6 files changed
+177
-17
No files matched your search
@@ -10936,6 +10936,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:
|
||||
@@ -10943,14 +10944,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_;
|
||||
};
|
||||
|
||||
@@ -13580,7 +13580,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
|
||||
@@ -13599,7 +13599,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;
|
||||
}
|
||||
|
||||
@@ -13689,12 +13700,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;
|
||||
@@ -23870,14 +23889,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; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user