diff --git a/README-websocket.md b/README-websocket.md index f84c7e46..ea2a05d5 100644 --- a/README-websocket.md +++ b/README-websocket.md @@ -66,7 +66,7 @@ enum ReadResult : int { Returned by `read()`. Since `Fail` is `0`, the result works naturally in boolean contexts — `while (ws.read(msg))` continues until the connection closes. When you need to distinguish text from binary, check the return value directly. -`Timeout` is only returned for a read timeout you set yourself with `set_read_timeout()`. It means the timeout elapsed on a message boundary: nothing was consumed and the connection is still open, so you can send on it and read again. The compile-time defaults (`CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND`, 300 seconds on the server; a client waits forever) are a backstop against a peer that has gone quiet, not a request for control: when one of them elapses, `read()` returns `Fail` and closes the connection, so code that never calls `set_read_timeout()` can keep using `while (ws.read(msg))`. +`Timeout` is only returned for a read timeout you set yourself with `set_read_timeout()`. It means the timeout elapsed and the connection is still open, so you can send on it and read again. It can land anywhere, inside a message too: what was read of the message is kept, and the next `read()` goes on from there. The compile-time defaults (`CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND`, 300 seconds on the server; a client waits forever) are a backstop against a peer that has gone quiet, not a request for control: when one of them elapses, `read()` returns `Fail` and closes the connection, so code that never calls `set_read_timeout()` can keep using `while (ws.read(msg))`. **`msg` is left untouched on `Timeout`.** Because `Timeout` is non-zero, `while (ws.read(msg))` keeps looping — with the *previous* message still in `msg`. Once you set a read timeout, test the result instead: @@ -231,7 +231,7 @@ void set_connection_timeout(const std::chrono::duration &duration); void set_websocket_ping_interval(time_t sec); void set_websocket_max_missed_pongs(int count); -// Socket - the same setters Client has +// Socket - the same setters Client has (TCP_NODELAY is on by default here) void set_tcp_nodelay(bool on); void set_address_family(int family); void set_ipv6_v6only(bool on); @@ -511,6 +511,7 @@ if (ws.connect()) { | `CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND` | `5` | Timeout for waiting peer's Close response (seconds) | | `CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND` | `30` | Automatic Ping interval for heartbeat (seconds) | | `CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS` | `0` (disabled) | Close the connection after N consecutive unacked pings | +| `CPPHTTPLIB_WEBSOCKET_TCP_NODELAY` | `true` | Set TCP_NODELAY on WebSocket connections, so small messages do not wait on Nagle's algorithm | ### Runtime Ping Interval diff --git a/docs-src/pages/en/cookbook/w06-websocket-timeouts.md b/docs-src/pages/en/cookbook/w06-websocket-timeouts.md index c8e4f4ca..e2ad8555 100644 --- a/docs-src/pages/en/cookbook/w06-websocket-timeouts.md +++ b/docs-src/pages/en/cookbook/w06-websocket-timeouts.md @@ -42,7 +42,7 @@ ws.set_write_timeout(10s); ## What the read timeout means -`set_read_timeout()` applies to a single `read()` call. If no message arrives within that time, `read()` returns `ReadResult::Timeout`: **the connection is still open** and nothing was consumed, so you can send on it and read again. That is what separates it from `ReadResult::Fail`, which means the connection is gone. +`set_read_timeout()` applies to a single `read()` call. If no whole message arrives within that time, `read()` returns `ReadResult::Timeout`: **the connection is still open**, and what was read of a message is kept for the next `read()`, so you can send on it and read again. That is what separates it from `ReadResult::Fail`, which means the connection is gone. This is what lets one thread own a connection in both directions: @@ -67,7 +67,7 @@ Without a read timeout, `read()` blocks until a message arrives, so the thread h Two things to know about `Timeout`: - It leaves `msg` untouched, and it is non-zero. So `while (ws.read(msg))` is not usable once a read timeout is set — the loop would keep running with the *previous* message still in `msg`. -- It is only reported on a message boundary. If the timeout elapses partway through a fragmented message, that message cannot be resumed and `read()` returns `Fail`. +- It can land anywhere, partway through a message too. What was read of the message is kept, and the next `read()` goes on from there, so a message that arrives slowly or in fragments still comes out whole. For connections where long idle periods are normal — waiting on notifications, for example — either leave the read timeout unset, or treat `Timeout` as the no-op it is and keep looping. diff --git a/docs-src/pages/ja/cookbook/w06-websocket-timeouts.md b/docs-src/pages/ja/cookbook/w06-websocket-timeouts.md index 4db3411c..e2fcd61f 100644 --- a/docs-src/pages/ja/cookbook/w06-websocket-timeouts.md +++ b/docs-src/pages/ja/cookbook/w06-websocket-timeouts.md @@ -42,7 +42,7 @@ ws.set_write_timeout(10s); ## 読み取りタイムアウトの意味 -`set_read_timeout()`は「1回の`read()`呼び出し」に対するタイムアウトです。メッセージが届かないまま指定時間が経過すると`read()`は`ReadResult::Timeout`を返します。このとき**接続は開いたまま**で、1バイトも読み進めていないので、そのまま送信して読み直せます。接続が失われたことを意味する`ReadResult::Fail`とはここが違います。 +`set_read_timeout()`は「1回の`read()`呼び出し」に対するタイムアウトです。メッセージがそろわないまま指定時間が経過すると`read()`は`ReadResult::Timeout`を返します。このとき**接続は開いたまま**で、途中まで読んだ分は次の`read()`に引き継がれるので、そのまま送信して読み直せます。接続が失われたことを意味する`ReadResult::Fail`とはここが違います。 1本の接続を1つのスレッドで双方向に扱えるのはこのためです。 @@ -67,7 +67,7 @@ while (ws.is_open()) { `Timeout`について2点あります。 - `msg`は書き換えられません。値も0以外なので、読み取りタイムアウトを設定した状態で`while (ws.read(msg))`と書くと、**前回のメッセージ**が`msg`に残ったままループが回り続けます。 -- 報告されるのはメッセージの境界だけです。分割されたメッセージの途中でタイムアウトした場合、そのメッセージは再開できないので`read()`は`Fail`を返します。 +- メッセージの途中でも報告されます。それまでに読んだ分は残り、次の`read()`はその続きから読むので、ゆっくり届くメッセージや分割されたメッセージも、そろった形で受け取れます。 通知の待受のように長時間メッセージが来ないことが正常な接続では、読み取りタイムアウトを設定しないままにするか、`Timeout`を「まだ何も来ていない」印として扱ってループを続けてください。 diff --git a/httplib.h b/httplib.h index bfcb1137..3e1d7337 100644 --- a/httplib.h +++ b/httplib.h @@ -291,6 +291,13 @@ static_assert(CPPHTTPLIB_MAX_LINE_LENGTH > 0, #define CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS 0 #endif +// A WebSocket connection carries small messages each way, which Nagle's +// algorithm would hold back for the peer's delayed ACK (tens of milliseconds +// on Linux), so it sets TCP_NODELAY unless this is false. +#ifndef CPPHTTPLIB_WEBSOCKET_TCP_NODELAY +#define CPPHTTPLIB_WEBSOCKET_TCP_NODELAY true +#endif + /* * Headers */ @@ -4701,6 +4708,43 @@ private: #endif }; +namespace impl { + +bool is_valid_utf8(const std::string &s); + +// Three states: a frame, a failure (the connection cannot be read on), or a +// timeout with the stream still usable. +enum class FrameRead { Ok, Fail, Timeout }; + +// A frame read in as many calls as it takes. A timeout leaves what was read +// so far in place, and the next read() goes on from there, so the stream is +// usable after a timeout wherever in a frame it lands. +class FrameReader { +public: + FrameRead read(Stream &strm, Opcode &opcode, std::string &payload, bool &fin, + bool expect_masked, size_t max_len); + // Whether part of a frame has been read and the rest is still to come + bool begun() const { return have_ > 0; } + +private: + void reset(); + + uint8_t head_[14] = {0}; // FIN + opcode, MASK + length, its extension, key + size_t have_ = 0; // bytes of the header read so far + size_t need_ = 2; // bytes of the header, once the second is read + bool sized_ = false; // the header is whole and its length checked + size_t len_ = 0; // the payload's length, once sized_ + std::string payload_; +}; + +// A whole frame read in one go: a timeout before any of it came is reported, +// and one that cuts it short is a failure. +FrameRead read_websocket_frame(Stream &strm, Opcode &opcode, + std::string &payload, bool &fin, + bool expect_masked, size_t max_len); + +} // namespace impl + class WebSocket { public: WebSocket(const WebSocket &) = delete; @@ -4718,6 +4762,8 @@ public: // Bound how long read() waits before returning Timeout. 0 waits forever. // A server handler owns its connection's timeout this way; a client sets it // through WebSocketClient. Safe to call while another thread is in read(). + // It lands anywhere, inside a message too: what was read of it is kept, + // and the next read() goes on from there. // // Only a timeout set here is reported as Timeout. The compile-time default // (CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND) is a backstop rather @@ -4782,10 +4828,16 @@ private: std::atomic read_timeout_set_{false}; std::mutex write_mutex_; // Owned by whichever thread is parsing frames off strm_. Only one thread - // may do so: read_websocket_frame() reads a payload until it has the whole - // declared length, so a second parser stealing bytes silently corrupts the - // message the first one is assembling. + // may do so: a frame is read until it has the whole declared length, + // across several read() calls when a timeout lands inside it (frames_ + // below), so a second parser stealing bytes silently corrupts the message + // the first one is assembling. std::mutex read_mutex_; + // What read() has of the frame and the message it has not finished, kept + // across a timeout. Guarded by read_mutex_. + impl::FrameReader frames_; + std::string message_; + ReadResult message_kind_ = Fail; // Text or Binary while one is assembled std::thread ping_thread_; std::mutex ping_mutex_; std::condition_variable ping_cv_; @@ -4900,7 +4952,7 @@ private: CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND; int websocket_max_missed_pongs_ = CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS; int address_family_ = AF_UNSPEC; - bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + bool tcp_nodelay_ = CPPHTTPLIB_WEBSOCKET_TCP_NODELAY; bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; SocketOptions socket_options_ = nullptr; time_t connection_timeout_sec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND; @@ -4974,23 +5026,15 @@ inline void WebSocketClient::set_connection_timeout( }); } -namespace impl { - -bool is_valid_utf8(const std::string &s); - -// Three states, because a failure that consumed bytes and one that consumed -// none are not the same thing: the first has left the stream in the middle of -// a frame and the connection cannot be reused, the second can just be retried. -enum class FrameRead { Ok, Fail, Timeout }; - -FrameRead read_websocket_frame(Stream &strm, Opcode &opcode, - std::string &payload, bool &fin, - bool expect_masked, size_t max_len); - -} // namespace impl - } // namespace ws +namespace detail { + +bool write_websocket_frame(Stream &strm, ws::Opcode opcode, const char *data, + size_t len, bool fin, bool mask); + +} // namespace detail + // ---------------------------------------------------------------------------- /* @@ -5916,67 +5960,59 @@ inline void fill_random_bytes(unsigned char *out, size_t n) { inline bool write_websocket_frame(Stream &strm, ws::Opcode opcode, const char *data, size_t len, bool fin, bool mask) { - // First byte: FIN + opcode - uint8_t header[2]; - header[0] = static_cast((fin ? 0x80 : 0x00) | - (static_cast(opcode) & 0x0F)); - - // Second byte: MASK + payload length + // The header (FIN + opcode, MASK + length, the extended length, the mask + // key: 14 bytes at most) goes out in one write with the payload's first + // chunk, so that under TCP_NODELAY a small frame is one segment, not three. + uint8_t header[14]; + size_t header_len = 0; + header[header_len++] = static_cast( + (fin ? 0x80 : 0x00) | (static_cast(opcode) & 0x0F)); + uint8_t mask_bit = mask ? 0x80 : 0x00; if (len < 126) { - header[1] = static_cast(len); - if (mask) { header[1] |= 0x80; } - if (!write_data(strm, reinterpret_cast(header), 2)) { - return false; - } + header[header_len++] = static_cast(len) | mask_bit; } else if (len <= 0xFFFF) { - header[1] = 126; - if (mask) { header[1] |= 0x80; } - if (!write_data(strm, reinterpret_cast(header), 2)) { - return false; - } - uint8_t ext[2]; - ext[0] = static_cast((len >> 8) & 0xFF); - ext[1] = static_cast(len & 0xFF); - if (!write_data(strm, reinterpret_cast(ext), 2)) { return false; } + header[header_len++] = 126 | mask_bit; + header[header_len++] = static_cast((len >> 8) & 0xFF); + header[header_len++] = static_cast(len & 0xFF); } else { - header[1] = 127; - if (mask) { header[1] |= 0x80; } - if (!write_data(strm, reinterpret_cast(header), 2)) { - return false; - } - uint8_t ext[8]; + header[header_len++] = 127 | mask_bit; for (int i = 7; i >= 0; i--) { - ext[7 - i] = + header[header_len++] = static_cast((static_cast(len) >> (i * 8)) & 0xFF); } - if (!write_data(strm, reinterpret_cast(ext), 8)) { return false; } } - + uint8_t mask_key[4] = {0}; if (mask) { - // Generate random mask key - uint8_t mask_key[4]; fill_random_bytes(mask_key, sizeof(mask_key)); - if (!write_data(strm, reinterpret_cast(mask_key), 4)) { - return false; - } - - // Write masked payload in chunks - const size_t chunk_size = 4096; - std::vector buf((std::min)(len, chunk_size)); - for (size_t offset = 0; offset < len; offset += chunk_size) { - size_t n = (std::min)(chunk_size, len - offset); - for (size_t i = 0; i < n; i++) { - buf[i] = - data[offset + i] ^ static_cast(mask_key[(offset + i) % 4]); - } - if (!write_data(strm, buf.data(), n)) { return false; } - } - } else { - if (len > 0) { - if (!write_data(strm, data, len)) { return false; } - } + std::copy(mask_key, mask_key + 4, header + header_len); + header_len += 4; } + // `n` bytes of the payload from `offset`, masked if they are to be, at `out` + auto put = [&](char *out, size_t offset, size_t n) { + for (size_t i = 0; i < n; i++) { + out[i] = mask ? data[offset + i] ^ + static_cast(mask_key[(offset + i) % 4]) + : data[offset + i]; + } + }; + + const size_t chunk_size = 4096; + size_t first = (std::min)(len, chunk_size); + std::vector buf(header_len + first); + std::copy(header, header + header_len, buf.begin()); + put(buf.data() + header_len, 0, first); + if (!write_data(strm, buf.data(), buf.size())) { return false; } + + // The rest, masked a chunk at a time, or else as it is + if (!mask) { + return first == len || write_data(strm, data + first, len - first); + } + for (size_t offset = first; offset < len; offset += chunk_size) { + size_t n = (std::min)(chunk_size, len - offset); + put(buf.data(), offset, n); + if (!write_data(strm, buf.data(), n)) { return false; } + } return true; } @@ -5985,109 +6021,106 @@ inline bool write_websocket_frame(Stream &strm, ws::Opcode opcode, namespace ws { namespace impl { -// Read exactly `size` bytes. Stream::read may return less than asked for -- it -// hands back whatever its buffer already holds -- so every multi-byte field has -// to loop. Reading a 2-byte header with a single read() fails whenever the -// header straddles the read buffer's boundary. -// -// Timeout is reported only when nothing at all was consumed. Once a byte has -// been taken the stream sits mid-field and cannot be resumed, so a timeout -// there is a failure like any other. (When read() fails it always records why, +inline void FrameReader::reset() { + have_ = 0; + need_ = 2; + sized_ = false; + len_ = 0; + payload_.clear(); +} + +// Stream::read may return less than asked for -- it hands back whatever its +// buffer already holds -- so each part of the frame is read in a loop, a byte +// at a time if that is how it comes. (When read() fails it always records why, // so the error belongs to this call and not to an earlier one.) -inline FrameRead read_exact(Stream &strm, void *buf, size_t size) { - auto p = static_cast(buf); - size_t total = 0; - while (total < size) { - auto n = strm.read(p + total, size - total); - if (n <= 0) { - auto timed_out = total == 0 && strm.get_error() == Error::Timeout; - return timed_out ? FrameRead::Timeout : FrameRead::Fail; +inline FrameRead FrameReader::read(Stream &strm, Opcode &opcode, + std::string &payload, bool &fin, + bool expect_masked, size_t max_len) { + // A timeout keeps what was read; anything else ends the frame + auto stopped = [&]() { + if (strm.get_error() == Error::Timeout) { return FrameRead::Timeout; } + reset(); + return FrameRead::Fail; + }; + auto fail = [&]() { + reset(); + return FrameRead::Fail; + }; + + while (have_ < need_) { + auto n = strm.read(reinterpret_cast(head_) + have_, need_ - have_); + if (n <= 0) { return stopped(); } + have_ += static_cast(n); + if (have_ < 2 || need_ > 2) { continue; } + + // RSV1, RSV2, RSV3 must be 0 when no extension is negotiated + if (head_[0] & 0x70) { return fail(); } + // RFC 6455 Section 5.5: control frames MUST NOT be fragmented and + // MUST have a payload length of 125 bytes or less + auto len7 = head_[1] & 0x7F; + if ((head_[0] & 0x08) && (!(head_[0] & 0x80) || len7 > 125)) { + return fail(); } - total += static_cast(n); + auto masked = (head_[1] & 0x80) != 0; + if (masked != expect_masked) { return fail(); } + // The extended length and the mask key follow + need_ = 2 + (len7 == 126 ? 2 : len7 == 127 ? 8 : 0) + (masked ? 4 : 0); } + + if (!sized_) { + uint64_t len = head_[1] & 0x7F; + if (len == 126) { + len = (static_cast(head_[2]) << 8) | head_[3]; + } else if (len == 127) { + // RFC 6455 Section 5.2: the most significant bit MUST be 0 + if (head_[2] & 0x80) { return fail(); } + len = 0; + for (size_t i = 2; i < 10; i++) { + len = (len << 8) | head_[i]; + } + } + // A max_len of 0 means no limit, as payload_max_length does, and a length + // size_t cannot hold (32-bit builds) is refused either way. + if ((max_len > 0 && len > max_len) || !detail::fits_in_size_t(len)) { + return fail(); + } + len_ = static_cast(len); + sized_ = true; + } + + // Grow the payload only as it arrives, since a header alone can announce + // max_len bytes that never come. + while (payload_.size() < len_) { + auto offset = payload_.size(); + auto n = (std::min)(len_ - offset, CPPHTTPLIB_RECV_BUFSIZ); + payload_.resize(offset + n); + auto got = strm.read(&payload_[offset], n); + if (got <= 0) { + payload_.resize(offset); + return stopped(); + } + payload_.resize(offset + static_cast(got)); + } + + if (head_[1] & 0x80) { + const auto *mask_key = head_ + need_ - 4; + for (size_t i = 0; i < payload_.size(); i++) { + payload_[i] ^= static_cast(mask_key[i % 4]); + } + } + fin = (head_[0] & 0x80) != 0; + opcode = static_cast(head_[0] & 0x0F); + payload = std::move(payload_); + reset(); return FrameRead::Ok; } inline FrameRead read_websocket_frame(Stream &strm, Opcode &opcode, std::string &payload, bool &fin, bool expect_masked, size_t max_len) { - // Read first 2 bytes. This is the only read that may report a timeout: it - // sits on a frame boundary, where nothing has been consumed yet. - uint8_t header[2]; - FrameRead first = read_exact(strm, header, 2); - if (first != FrameRead::Ok) { return first; } - - fin = (header[0] & 0x80) != 0; - - // RSV1, RSV2, RSV3 must be 0 when no extension is negotiated - if (header[0] & 0x70) { return FrameRead::Fail; } - - opcode = static_cast(header[0] & 0x0F); - bool masked = (header[1] & 0x80) != 0; - uint64_t payload_len = header[1] & 0x7F; - - // RFC 6455 Section 5.5: control frames MUST NOT be fragmented and - // MUST have a payload length of 125 bytes or less - bool is_control = (static_cast(opcode) & 0x08) != 0; - if (is_control) { - if (!fin) { return FrameRead::Fail; } - if (payload_len > 125) { return FrameRead::Fail; } - } - - if (masked != expect_masked) { return FrameRead::Fail; } - - // Extended payload length - if (payload_len == 126) { - uint8_t ext[2]; - if (read_exact(strm, ext, 2) != FrameRead::Ok) { return FrameRead::Fail; } - payload_len = (static_cast(ext[0]) << 8) | ext[1]; - } else if (payload_len == 127) { - uint8_t ext[8]; - if (read_exact(strm, ext, 8) != FrameRead::Ok) { return FrameRead::Fail; } - // RFC 6455 Section 5.2: the most significant bit MUST be 0 - if (ext[0] & 0x80) { return FrameRead::Fail; } - payload_len = 0; - for (int i = 0; i < 8; i++) { - payload_len = (payload_len << 8) | ext[i]; - } - } - - // A max_len of 0 means no limit, as payload_max_length does, and a length - // size_t cannot hold (32-bit builds) is refused either way. - if ((max_len > 0 && payload_len > max_len) || - !detail::fits_in_size_t(payload_len)) { - return FrameRead::Fail; - } - - // Read mask key if present - uint8_t mask_key[4] = {0}; - if (masked) { - if (read_exact(strm, mask_key, 4) != FrameRead::Ok) { - return FrameRead::Fail; - } - } - - // Grow the payload only as it arrives, since a header alone can announce - // max_len bytes that never come. - payload.clear(); - auto len = static_cast(payload_len); - while (payload.size() < len) { - auto offset = payload.size(); - auto n = (std::min)(len - offset, CPPHTTPLIB_RECV_BUFSIZ); - payload.resize(offset + n); - if (read_exact(strm, &payload[offset], n) != FrameRead::Ok) { - return FrameRead::Fail; - } - } - - // Unmask if needed - if (masked) { - for (size_t i = 0; i < payload.size(); i++) { - payload[i] ^= static_cast(mask_key[i % 4]); - } - } - - return FrameRead::Ok; + FrameReader reader; + auto r = reader.read(strm, opcode, payload, fin, expect_masked, max_len); + return r == FrameRead::Timeout && reader.begun() ? FrameRead::Fail : r; } // RFC 6455 §5.5.1, §7.4: a Close payload is empty, or a status code that may @@ -15568,6 +15601,9 @@ 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(); }; + if (CPPHTTPLIB_WEBSOCKET_TCP_NODELAY) { + set_socket_opt(strm.socket(), IPPROTO_TCP, TCP_NODELAY, 1); + } #ifdef CPPHTTPLIB_SSL_ENABLED if (req.ssl) { @@ -23851,21 +23887,28 @@ inline ReadResult WebSocket::read(std::string &msg) { close(CloseStatus::GoingAway, "server stopping"); return Fail; }; + // close() ends the read, even while the peer keeps sending fragments while (!closed_) { Opcode opcode; std::string payload; bool fin; - impl::FrameRead r = - impl::read_websocket_frame(strm_, opcode, payload, fin, is_server_, - CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH); + // While a message is assembled, only what is left of the limit, but room + // for a control frame, which may come between its fragments. + auto max_len = + CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH == 0 || message_kind_ == Fail + ? size_t(CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH) + : (std::max)(size_t(125), CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH - + message_.size()); + auto r = frames_.read(strm_, opcode, payload, fin, is_server_, max_len); 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 - // compile-time default is a backstop against a peer gone quiet, and - // elapsing it closes the connection so a plain `while (ws.read(msg))` loop - // ends. A wait that a pong timeout cut short finds the connection closed. + // A timeout leaves what was read of the frame and of the message for the + // next read(), so the connection is still usable wherever it landed: hand + // control back without closing it. That is only useful to a caller who + // asked for the timeout; the compile-time default is a backstop against a + // peer gone quiet, and elapsing it closes the connection so a plain + // `while (ws.read(msg))` loop ends. A wait that a pong timeout cut short + // finds the connection closed. if (r == impl::FrameRead::Timeout && read_timeout_set_ && !closed_) { return Timeout; } @@ -23874,69 +23917,43 @@ inline ReadResult WebSocket::read(std::string &msg) { return Fail; } - switch (opcode) { - case Opcode::Text: - case Opcode::Binary: { - auto result = opcode == Opcode::Text ? Text : Binary; - msg = std::move(payload); - - // Handle fragmentation - if (!fin) { - while (true) { - // As in the loop over frames above, close() ends the read even while - // the peer keeps sending fragments - if (closed_) { return Fail; } - Opcode cont_opcode; - std::string cont_payload; - bool cont_fin; - // Only what is left of the limit, but room for a control frame, - // which may come between the fragments. - auto max_len = - CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH == 0 - ? size_t(0) - : (std::max)(size_t(125), - CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH - - msg.size()); - // A timeout is not reportable here: half of a fragmented message is - // already in `msg` and read() has no way to resume it, so it is a - // failure like any other. Timeouts are only ever seen on a message - // boundary. - 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; - } - // RFC 6455 5.4: the fragments are continuation frames, with only - // control frames between them. - if (cont_opcode != Opcode::Continuation) { - if (handle_control(cont_opcode, cont_payload)) { continue; } - return Fail; - } - msg += cont_payload; - if (CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH > 0 && - msg.size() > CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH) { - closed_ = true; - return Fail; - } - if (cont_fin) { break; } - } - } - // RFC 6455 Section 5.6: text frames must contain valid UTF-8 - if (result == Text && !impl::is_valid_utf8(msg)) { - // close() takes the read lock to wait for the peer's Close reply, so - // it must not run while this thread still holds it. - read_lock.unlock(); - close(CloseStatus::InvalidPayload, "invalid UTF-8"); - return Fail; - } - return result; - } - default: + auto continues = opcode == Opcode::Continuation; + if (opcode != Opcode::Text && opcode != Opcode::Binary && !continues) { if (handle_control(opcode, payload)) { continue; } return Fail; } + // RFC 6455 5.4: a message is a Text or Binary frame and the continuation + // frames after it, with only control frames between them. + if (continues == (message_kind_ == Fail)) { + closed_ = true; + return Fail; + } + if (continues) { + message_ += payload; + } else { + message_ = std::move(payload); + message_kind_ = opcode == Opcode::Text ? Text : Binary; + } + if (CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH > 0 && + message_.size() > CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH) { + closed_ = true; + return Fail; + } + if (!fin) { continue; } + + auto result = message_kind_; + msg = std::move(message_); + message_.clear(); + message_kind_ = Fail; + // RFC 6455 Section 5.6: text frames must contain valid UTF-8 + if (result == Text && !impl::is_valid_utf8(msg)) { + // close() takes the read lock to wait for the peer's Close reply, so + // it must not run while this thread still holds it. + read_lock.unlock(); + close(CloseStatus::InvalidPayload, "invalid UTF-8"); + return Fail; + } + return result; } return Fail; } @@ -23989,11 +24006,12 @@ inline void WebSocket::close(CloseStatus status, const std::string &reason) { auto deadline = detail::deadline_after(CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND, 0); + // From where the last read() left the stream, inside a frame as it may be Opcode op; std::string resp; bool fin; while (std::chrono::steady_clock::now() < deadline && - impl::read_websocket_frame(strm_, op, resp, fin, is_server_, 125) == + frames_.read(strm_, op, resp, fin, is_server_, 125) == impl::FrameRead::Ok) { if (op == Opcode::Close) { break; } } diff --git a/test/test.cc b/test/test.cc index 355a74f9..faf714c3 100644 --- a/test/test.cc +++ b/test/test.cc @@ -29894,6 +29894,155 @@ TEST(WebSocketFrameTest, MultiByteFieldsSplitAcrossReads) { EXPECT_EQ(payload, body); } +// Keeps what is written, counting the writes, and reads it back. +class WsWriteCountingStream : public Stream { +public: + bool is_readable() const override { return true; } + bool wait_readable() const override { return true; } + bool wait_writable() const override { return true; } + ssize_t read(char *ptr, size_t size) override { + auto n = (std::min)(size, data_.size() - pos_); + if (n == 0) { return 0; } + memcpy(ptr, data_.data() + pos_, n); + pos_ += n; + return static_cast(n); + } + ssize_t write(const char *ptr, size_t size) override { + writes++; + data_.append(ptr, size); + return static_cast(size); + } + void get_remote_ip_and_port(std::string &ip, int &port) const override { + ip = "127.0.0.1"; + port = 0; + } + void get_local_ip_and_port(std::string &ip, int &port) const override { + ip = "127.0.0.1"; + port = 0; + } + socket_t socket() const override { return INVALID_SOCKET; } + time_t duration() const override { return 0; } + + int writes = 0; + +private: + std::string data_; + size_t pos_ = 0; +}; + +TEST(WebSocketFrameTest, AFrameGoesOutWithItsHeaderInOneWrite) { + // Under TCP_NODELAY each write is a segment of its own. Each length form, + // masked and not, reads back as written; a payload over one chunk follows + // its first in more writes. + for (auto len : {size_t(5), size_t(200), size_t(70000)}) { + for (auto mask : {false, true}) { + std::string body(len, '\0'); + for (size_t i = 0; i < len; i++) { + body[i] = static_cast(i % 251); + } + WsWriteCountingStream strm; + ASSERT_TRUE(detail::write_websocket_frame( + strm, ws::Opcode::Binary, body.data(), body.size(), true, mask)); + if (len <= 4096) { EXPECT_EQ(1, strm.writes) << len << " " << mask; } + + ws::Opcode opcode; + std::string payload; + bool fin = false; + ASSERT_EQ( + ws::impl::read_websocket_frame(strm, opcode, payload, fin, mask, 0), + ws::impl::FrameRead::Ok) + << len << " " << mask; + EXPECT_TRUE(fin); + EXPECT_EQ(ws::Opcode::Binary, opcode); + EXPECT_EQ(body, payload) << len << " " << mask; + } + } +} + +// Hands out its pieces one read at a time, timing out between each two. +class WsTimeoutBetweenPiecesStream : public Stream { +public: + explicit WsTimeoutBetweenPiecesStream(std::vector pieces) + : pieces_(std::move(pieces)) {} + bool is_readable() const override { return true; } + bool wait_readable() const override { return true; } + bool wait_writable() const override { return true; } + ssize_t read(char *ptr, size_t size) override { + if (time_out_) { + time_out_ = false; + error_ = Error::Timeout; + return -1; + } + if (i_ >= pieces_.size()) { + error_ = Error::ConnectionClosed; + return 0; + } + auto &piece = pieces_[i_]; + auto n = (std::min)(size, piece.size() - at_); + memcpy(ptr, piece.data() + at_, n); + at_ += n; + if (at_ == piece.size()) { + i_++; + at_ = 0; + time_out_ = true; + } + return static_cast(n); + } + ssize_t write(const char *, size_t size) override { + return static_cast(size); + } + void get_remote_ip_and_port(std::string &ip, int &port) const override { + ip = "127.0.0.1"; + port = 0; + } + void get_local_ip_and_port(std::string &ip, int &port) const override { + ip = "127.0.0.1"; + port = 0; + } + socket_t socket() const override { return INVALID_SOCKET; } + time_t duration() const override { return 0; } + +private: + std::vector pieces_; + size_t i_ = 0; + size_t at_ = 0; + bool time_out_ = false; +}; + +TEST(WebSocketFrameTest, ATimeoutInsideAFrameKeepsWhatWasRead) { + // FIN + Text, masked with an all-zero key: "Hello" goes out verbatim + std::string frame = "\x81\x85"; + frame.append(4, '\0'); + frame += "Hello"; + // The header's first byte, then its second with half the mask key, then + // the rest + std::vector pieces = {frame.substr(0, 1), frame.substr(1, 3), + frame.substr(4)}; + + WsTimeoutBetweenPiecesStream strm(pieces); + ws::impl::FrameReader reader; + ws::Opcode opcode; + std::string payload; + bool fin = false; + for (int i = 0; i < 2; i++) { + EXPECT_EQ(ws::impl::FrameRead::Timeout, + reader.read(strm, opcode, payload, fin, true, 0)); + EXPECT_TRUE(reader.begun()); + } + ASSERT_EQ(ws::impl::FrameRead::Ok, + reader.read(strm, opcode, payload, fin, true, 0)); + EXPECT_FALSE(reader.begun()); + EXPECT_TRUE(fin); + EXPECT_EQ(ws::Opcode::Text, opcode); + EXPECT_EQ("Hello", payload); + + // Read in one go, the frame cannot be gone on with after a timeout + WsTimeoutBetweenPiecesStream again(pieces); + EXPECT_EQ( + ws::impl::FrameRead::Fail, + ws::impl::read_websocket_frame(again, opcode, payload, fin, true, 0)); +} + TEST(WebSocketFrameTest, ZeroMaxLenMeansNoLimit) { std::string body(2000, 'x'); std::string frame; @@ -30403,7 +30552,7 @@ TEST(WebSocketTest, CloseDoesNotStealBytesFromConcurrentRead) { }); // Wait until the peer stalls mid-payload, so the reader thread is parked - // inside read_websocket_frame() when close() runs. + // inside a frame when close() runs. wait_for(peer_stalled); ws.close(); reader.join(); @@ -30807,13 +30956,16 @@ TEST(WebSocketTest, StopEndsAWaitingReadOverTLS) { // 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) { +// of one: `sent` is what the client sends first. A `bounded` handler's reads +// time out inside the message as well, and the stop ends them too. +static void check_stop_closes_with_going_away(const std::string &sent, + bool bounded = false) { Server svr; svr.set_websocket_ping_interval(0); svr.WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) { + if (bounded) { ws.set_read_timeout(0, 10000); } std::string msg; - while (ws.read(msg)) {} + while (ws.read(msg) != ws::Fail) {} }); auto port = svr.bind_to_any_port("127.0.0.1"); @@ -30857,6 +31009,76 @@ TEST(WebSocketTest, StopMidMessageClosesWithGoingAway) { check_stop_closes_with_going_away(masked_frame(0x01, "half")); } +TEST(WebSocketTest, StopMidBoundedMessageClosesWithGoingAway) { + check_stop_closes_with_going_away(masked_frame(0x01, "half"), + /*bounded=*/true); +} + +// A handler that bounds its reads gets control back within its timeout even +// while a message arrives in pieces, and the message whole once the last has +// come: what was read of it is kept across the timeouts. Each of `pieces` is +// sent a while after the one before, much longer than the handler's timeout. +static void check_bounded_read_resumes(const std::vector &pieces, + const std::string &expected) { + std::atomic done{false}; + std::string received; + std::chrono::steady_clock::duration longest{}; + + Server svr; + svr.set_websocket_ping_interval(0); + svr.WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) { + ws.set_read_timeout(0, 10000); + std::string msg; + ws::ReadResult r; + do { + auto began = std::chrono::steady_clock::now(); + r = ws.read(msg); + longest = (std::max)(longest, std::chrono::steady_clock::now() - began); + } while (r == ws::Timeout); + if (r == ws::Text) { received = msg; } + done = true; + }); + + 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); }); + for (const auto &piece : pieces) { + ASSERT_EQ(static_cast(piece.size()), + detail::send_socket(sock, piece.data(), piece.size(), 0)); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + } + + for (int i = 0; i < 500 && !done; i++) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + ASSERT_TRUE(done); + EXPECT_EQ(expected, received); + // Far under the 200 ms between the pieces, however loaded the machine + EXPECT_LT(longest, std::chrono::milliseconds(150)); +} + +TEST(WebSocketTest, BoundedReadResumesAFrameInPieces) { + // The header's first byte, then its second with half the mask key, then + // the rest: what a frame written in several writes may look like when + // Nagle's algorithm holds the later ones back for an ACK. + auto frame = masked_frame(0x81, "Hello"); // FIN + Text + check_bounded_read_resumes( + {frame.substr(0, 1), frame.substr(1, 3), frame.substr(4)}, "Hello"); +} + +TEST(WebSocketTest, BoundedReadResumesBetweenFragments) { + check_bounded_read_resumes( + {masked_frame(0x01, "Hel"), masked_frame(0x80, "lo")}, "Hello"); +} + TEST(WebSocketTest, AnswersAMalformedCloseWithProtocolError) { Server svr; svr.set_websocket_ping_interval(0);