Skip to content

Websockets - #46

Merged
RaphiaRa merged 13 commits into
mainfrom
websockets
Aug 11, 2026
Merged

Websockets#46
RaphiaRa merged 13 commits into
mainfrom
websockets

Conversation

@RaphiaRa

@RaphiaRa RaphiaRa commented Aug 11, 2026

Copy link
Copy Markdown
Owner
  • Implement basic websocket support (send + recv)
  • Refactor th_router as port of the websocket implementation
  • Fix a few bugs on the way
  • Add a websockets example
  • Update readme

Split th_router_add_route into th_router_find_or_create_segment
(path resolution) and the method-slot write, so websocket route
registration can reuse the trie walk without duplicating it.
- add th_ws_handler/th_ws_send/th_ws_close/th_route_ws public API,
  TH_ERR_BUSY, TH_CODE_SWITCHING_PROTOCOLS
- th_router_resolve/th_route_consume_trail take a th_str path and a
  capture callback instead of a th_request*, so path resolution no
  longer depends on a full request
- th_router_find_ws_route now takes a th_str path directly, matching
  resolve's new signature and letting WS route lookups skip building
  a th_request entirely
- add SHA-1, Base64, and RFC 6455 handshake/accept-key computation
- wire WS upgrade into th_http: 101 response on valid handshake,
  426 on non-handshake requests to WS routes, hand connection off
  to a new th_ws instance
- th_router: WS-only routes get a default GET handler so they
  don't 405; registering a real handler later overrides it
- fix Connection header value being discarded during parsing
- fix default error body reason phrase always showing "Unknown"
- th_ws_frame_parser: incremental RFC 6455 frame parser, mirrors
  th_request_parser's switch-based per-state dispatch
- th_ws now accumulates message payloads and dispatches DATA/CLOSE
  events; ping/pong are discarded without reaching the handler
- add TH_EPROTO system error code for protocol violations
- fix: th_route_init left ws_handler uninitialized, causing
  th_router_add_ws_route to spuriously fail on a fresh route

Sending frames is not implemented yet (th_ws_send/th_ws_close remain
TH_ERR_NOSUPPORT stubs).
Renames th_ws_msg_type to th_ws_type, and adds it as a parameter to
th_ws_handler so TH_WS_EVENT_DATA callers know the message's frame
opcode, not just its bytes.
- th_ws_send queues header+payload into a th_ring (growable chunked
  ring buffer) and drains it via th_conn_send; grows on overflow
  instead of rejecting, capped by TH_CONFIG_WS_SEND_MAX_LEN
- th_ws_frame_header_write encodes the frame header, shared so
  close/ping-pong replies can reuse it later
- fix: th_queue's _pop left a dangling tail pointer after removing
  the last item
- th_ws_close queues a CLOSE frame through the normal send path;
  TH_WS_EVENT_CLOSE fires and the connection tears down once it drains
- receiving a CLOSE now echoes one back before closing, instead of
  dropping the connection without completing the handshake
- th_ws_send/th_ws_close return TH_ERR_INVALID_ARG once closing
- fix: th_ring_chunk_write crashed on a NULL, zero-length payload
  (e.g. an empty CLOSE frame)
- fix typos
- Remove remark that OpenSSL is slow, should be quite OK by now
- Websocket support is now feature
@codecov-commenter

codecov-commenter commented Aug 11, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 87.24832% with 76 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/th_http.c 59.64% 15 Missing and 8 partials ⚠️
src/th_ws_frame_parser.c 85.27% 10 Missing and 9 partials ⚠️
src/th_router.c 82.45% 2 Missing and 8 partials ⚠️
src/th_ws.c 91.42% 3 Missing and 6 partials ⚠️
src/th_ring.c 95.00% 2 Missing and 2 partials ⚠️
src/th_server.c 0.00% 4 Missing ⚠️
src/th_base64.c 93.75% 1 Missing and 1 partial ⚠️
src/th_error.c 0.00% 2 Missing ⚠️
src/th_str.c 71.42% 1 Missing and 1 partial ⚠️
src/th_ws_handshake.c 96.87% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Codecov applies its own coverage checks independent of the uploaded
XML, so examples/tests/benchmarks need to be excluded there too.
@RaphiaRa
RaphiaRa merged commit d6ed677 into main Aug 11, 2026
3 checks passed
@RaphiaRa
RaphiaRa deleted the websockets branch August 11, 2026 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants