Expose the WebSocket close status and reason (#2695)

* Expose the WebSocket close status and reason

When the peer closed a WebSocket, read() returned Fail and the code and
reason of its Close frame were dropped after the reply, so a handler or
a WebSocketClient user could not tell a normal close from going away, an
application code, or a connection that ended without a Close at all.

Add close_status() and close_reason() to ws::WebSocket and
WebSocketClient. Once read() has returned Fail they report the code and
reason of the peer's Close (RFC 6455 7.1.5 and 7.1.6), NoStatus (1005)
when it carried no code, ProtocolError when it was malformed, and
Abnormal (1006) when none came. After a close() of our own they report
the peer's reply. Application codes (3000-4999) come back as a
CloseStatus too, as close() already takes them.

The status is set once, under the read mutex, with the reason written
before it, so the accessors may be called from any thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Return close_reason() by value and fold the handler-side close tests

WebSocketClient::close_reason() returned a reference into the connection
that connect() replaces, so a reference kept across a reconnect dangled.
Both close_reason() accessors now return a copy of the reason, which is
at most 123 bytes.

The six handler-side tests become one table-driven test on one server,
and the client test checks the reason still reads after a reconnect.

* Decode the close code in one place and fold the client close tests

is_valid_close_payload() and set_close_status() each decoded the code by
hand; they now share impl::close_code(). The client-side checks move
into the existing CloseHandshake, CloseWithStatus and
ClientCloseWithStatus tests, which already reach those states, and the
reconnect check becomes a static_assert on close_reason()'s type.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-11-26 19:31:11 -05:00
1 parent d5befb4326
commit f57e25ec92
5 files changed
+166 -5

No files matched your search

+16
View File
@@ -144,6 +144,14 @@ const Request &request() const;
// Check if the connection is still open
bool is_open() const;
// Why the connection ended, once read() has returned Fail: the code and reason
// of the peer's Close frame (any code, including 3000-4999, which are not
// named in CloseStatus), NoStatus (1005) for a Close without a code,
// ProtocolError (1002) for a malformed one, and Abnormal (1006) when the
// connection ended without one. After your own close(), the peer's reply.
CloseStatus close_status() const;
std::string close_reason() const;
// Bound how long read() waits before returning Timeout (0 waits forever).
// Safe to call while another thread is in read().
void set_read_timeout(time_t sec, time_t usec = 0);
@@ -200,6 +208,8 @@ bool send(const char *data, size_t len);
void close(CloseStatus status = CloseStatus::Normal,
const std::string &reason = "");
bool is_open() const;
CloseStatus close_status() const;
std::string close_reason() const;
// Authentication - the same setters Client has (Basic wins over Bearer).
void set_basic_auth(const std::string &username, const std::string &password);
@@ -378,6 +388,12 @@ ws.close(httplib::ws::CloseStatus::GoingAway, "shutting down");
// Server-side: close with a policy violation status
ws.close(httplib::ws::CloseStatus::PolicyViolation, "forbidden");
// Either side: why the peer closed, once read() has returned Fail
std::string msg;
while (ws.read(msg)) {}
auto code = static_cast<uint16_t>(ws.close_status()); // e.g. 1000, 1001, 4001
auto reason = ws.close_reason();
```
### Accessing the Upgrade Request