Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ IndentWidth: 4
InsertBraces: true
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 2
SeparateDefinitionBlocks: Always
NamespaceIndentation: All
PenaltyReturnTypeOnItsOwnLine: 1000
PointerAlignment: Left
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ set( IXWEBSOCKET_SOURCES
ixwebsocket/IXGetFreePort.cpp
ixwebsocket/IXGzipCodec.cpp
ixwebsocket/IXHttp.cpp
ixwebsocket/IXHpack.cpp
ixwebsocket/IXHttpClient.cpp
ixwebsocket/IXHttpServer.cpp
ixwebsocket/IXNetSystem.cpp
Expand All @@ -66,6 +67,9 @@ set( IXWEBSOCKET_SOURCES
ixwebsocket/IXSelectInterruptPipe.cpp
ixwebsocket/IXSelectInterruptEvent.cpp
ixwebsocket/IXSetThreadName.cpp
ixwebsocket/IXHttp2Connection.cpp
ixwebsocket/IXHttp2Stream.cpp
ixwebsocket/IXWebSocketStream.cpp
ixwebsocket/IXSocket.cpp
ixwebsocket/IXSocketConnect.cpp
ixwebsocket/IXSocketFactory.cpp
Expand Down Expand Up @@ -98,6 +102,7 @@ set( IXWEBSOCKET_HEADERS
ixwebsocket/IXExponentialBackoff.h
ixwebsocket/IXGetFreePort.h
ixwebsocket/IXGzipCodec.h
ixwebsocket/IXHpack.h
ixwebsocket/IXHttp.h
ixwebsocket/IXHttpClient.h
ixwebsocket/IXHttpServer.h
Expand All @@ -109,6 +114,9 @@ set( IXWEBSOCKET_HEADERS
ixwebsocket/IXSelectInterruptPipe.h
ixwebsocket/IXSelectInterruptEvent.h
ixwebsocket/IXSetThreadName.h
ixwebsocket/IXHttp2Connection.h
ixwebsocket/IXHttp2Stream.h
ixwebsocket/IXWebSocketStream.h
ixwebsocket/IXSocket.h
ixwebsocket/IXSocketConnect.h
ixwebsocket/IXSocketFactory.h
Expand Down
5 changes: 3 additions & 2 deletions docker/Dockerfile.h2-proxy
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
FROM debian:bookworm-slim AS build
RUN apt-get update && apt-get install -y --no-install-recommends \
g++ cmake ninja-build zlib1g-dev && rm -rf /var/lib/apt/lists/*
g++ cmake ninja-build zlib1g-dev libssl-dev && rm -rf /var/lib/apt/lists/*
WORKDIR /src
COPY CMakeLists.txt ixwebsocket-config.cmake.in ixwebsocket.pc.in ./
COPY CMake/ CMake/
COPY ixwebsocket/ ixwebsocket/
COPY test/h2_proxy/ test/h2_proxy/
RUN cmake -S test/h2_proxy -B build -GNinja -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_UNITY_BUILD=ON && cmake --build build --parallel 2
-DCMAKE_UNITY_BUILD=ON -DH2_PROXY_NATIVE_CLIENT=ON -DUSE_OPEN_SSL=ON && cmake --build build --parallel 2

FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
Expand All @@ -17,5 +17,6 @@ COPY test/h2_proxy/requirements.txt /requirements.txt
RUN python3 -m venv /venv && /venv/bin/pip install --no-cache-dir -r /requirements.txt
COPY test/h2_proxy/ /app/test/h2_proxy/
COPY --from=build /src/build/h2_proxy_server /app/build/h2-proxy/h2_proxy_server
COPY --from=build /src/build/h2_proxy_client /app/build/h2-proxy/h2_proxy_client
WORKDIR /app
ENTRYPOINT ["/venv/bin/python", "/app/test/h2_proxy/run.py"]
6 changes: 5 additions & 1 deletion docs/autobahn.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ missing cases, malformed reports, unknown statuses, non-strict results, and
closing failures fail the check. Process success alone is insufficient.

Reports are recreated under `build/autobahn/reports/client` and
`build/autobahn/reports/server`. Open each `index.html` for detailed results.
`build/autobahn/reports/server` (or `server-1`, `server-2`, etc. for large runs).
Open each `index.html` for detailed results. Server cases run in batches of at
most 64 with a fresh test runner, avoiding memory accumulation in the Python
suite. Every selected case still runs and every batch's complete report is checked;
the per-role timeout applies across all batches.
Container logs and the selected suite/image IDs are also saved under
`build/autobahn`. CI uploads these as the `autobahn-reports` artifact even when
tests fail. Each role has a 30-minute limit; the whole CI job has a 75-minute
Expand Down
11 changes: 8 additions & 3 deletions docs/h2-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ normally and checks that it joins its workers and exits successfully.

Coverage includes:

- Native IXWebSocket H2 client with compression on/off and both close directions.
- TLS certificate validation, ALPN, Extended CONNECT, and HTTP/2 success status.
- Forwarded Origin/custom headers, paths, and query strings.
- Empty, Unicode and binary messages, fragmentation, and ping/pong.
Expand All @@ -49,9 +50,13 @@ After the WebSocket Close exchange, nghttpx can retire a stream with
when both WebSocket Close frames have been exchanged. Premature resets and other
reset codes fail the test.

This is a protocol integration test using Python `h2` and `wsproto`, not browser
automation, an Autobahn run, or a performance benchmark. It adds no native HTTP/2
support to IXWebSocket. It runs separately from the regular unit suite.
The native C++ client runs through the proxy in four compression/close modes;
it also runs against an independent Python TLS/H2 peer for simultaneous large
transfers, blocked-send cancellation and ALPN rejection. The Python
`h2`/`wsproto` client then checks the proxy/backend flow,
including multiplexing. The C++ client currently uses one stream per connection.
This is protocol integration coverage, not browser automation, an Autobahn run,
or a performance benchmark. It runs separately from the regular unit suite.

## Native run and Envoy reproduction

Expand Down
114 changes: 114 additions & 0 deletions docs/http2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Experimental WebSocket client over HTTP/2

IXWebSocket has an opt-in, self-contained RFC 8441 client. It implements its own
HTTP/2 framing and HPACK codec and adds no HTTP/2 library dependency.
HTTP/1.1 remains the default.

```cpp
ix::WebSocket socket;
socket.setUrl("wss://example.com/echo");
socket.setHttp2(true);
socket.setOnMessageCallback([](const ix::WebSocketMessagePtr& message)
{
// Existing Open, Message, Ping/Pong, Error and Close events are unchanged.
});
socket.start();
```

The command-line client also supports explicit H2 mode:

```sh
ws connect wss://example.com/echo --http2
```

Configure before connecting, as with other WebSocket settings. HTTP/2 mode
requires `wss://`, TLS 1.2 or newer, ALPN `h2`, and a peer advertising
`SETTINGS_ENABLE_CONNECT_PROTOCOL`. It uses Extended CONNECT and accepts a
successful 2xx response. If negotiation fails, the connection fails explicitly;
it does not silently switch to HTTP/1.1. The normal automatic-reconnection
setting still applies.

This first implementation uses **one WebSocket stream per TCP/TLS connection**.
It does not pool connections or multiplex several WebSocket objects on one
connection. The HTTP client and the WebSocket/HTTP servers remain HTTP/1.1.
`connect(HttpClient&, timeout)` cannot transfer an HTTP/1.1 connection into this
mode. Cleartext HTTP/2 (`h2c`) is not implemented.

The TLS layer negotiates ALPN using the selected existing backend: OpenSSL/
LibreSSL, mbedTLS when built with ALPN support, Network.framework, or
SecureTransport. Existing backend certificate/trust limitations still apply.
Normal certificate and hostname verification remain enabled by default.

Existing WebSocket framing, masking, fragmentation, compression, message limits,
callbacks, ping/pong and close handling consume a `WebSocketStream` byte stream:

```text
WebSocketTransport
-> SocketStream -> Socket (TCP/TLS, after HTTP/1.1 Upgrade)
-> Http2Stream -> Http2Connection -> Socket (TCP/TLS)
```

The handshake transfers ownership of the appropriate stream to the transport.
`Socket` and its TLS subclasses keep their Unix socket responsibilities; neither
HTTP/2 class inherits from `Socket` or owns a placeholder descriptor.
`Http2Connection` owns HTTP/2 framing and flow control, while `Http2Stream`
exposes the WebSocket bytes through the same interface as `SocketStream`.
This keeps the existing one-connection-per-WebSocket behavior in both modes.

The connection handles SETTINGS, HEADERS/CONTINUATION, flow control, PING,
WINDOW_UPDATE, GOAWAY, RST_STREAM and stream termination. Server push is disabled.
HPACK responses support the static/dynamic tables and Huffman coding. Outgoing
fields are literal and never indexed, including credentials. Compressed and
decoded header blocks are bounded at 64 KiB, the dynamic table at 4 KiB, incoming
DATA at the advertised receive window, and pending control frames at 64.

Extra header names are encoded lowercase; Host is mapped to `:authority`.
HTTP/1.1-only headers such as Connection, Upgrade and Sec-WebSocket-Key are
rejected. Default User-Agent/Origin suppression and redirect credential handling
retain their existing APIs. The HTTP status and response headers remain available
on handshake failures.

This is an experimental transport path with a deliberately limited connection
model. Rebuild applications against the updated headers/library.

## Testing

```sh
make -f makefile.dev test_h2_proxy
```

The Docker integration test now also runs the native C++ client through nghttpx
with compression on/off and client/server-initiated closes. It checks Unicode,
empty/binary payloads, 256 KiB messages, ping/pong and close codes/reasons. The
independent Python H2 client continues to exercise the proxy and backend.
Reports and logs remain under `build/h2-proxy/runs/`.

Fast `IXHttp2Test` unit tests cover RFC 7541 vectors, the complete Huffman byte
alphabet, dynamic tables, malformed/truncated input, fragmented headers/reads,
partial writes, flow-control limits, cancellation and remote half closes.
They use an in-memory peer and have no deliberate sleeps or DNS dependencies.

For another TLS backend, configure the fixture separately, for example:

```sh
cmake -S test/h2_proxy -B build/h2-network \
-DH2_PROXY_NATIVE_CLIENT=ON -DUSE_NETWORK_FRAMEWORK=ON
cmake --build build/h2-network --parallel
```

With the test requirements installed in a venv, `test/h2_proxy/native_peer.py`
runs the native executable against an independent Python TLS/H2 peer:

```sh
python3 test/h2_proxy/native_peer.py \
build/h2-network/h2_proxy_client server.pem server.key ca.pem
```

The certificates should be a server certificate/key and its trusted CA, with
`127.0.0.1` in the server certificate's subject alternative names. This check
also exercises simultaneous large messages in both directions, cancellation while
flow control blocks sending, and compression on/off without requiring a proxy.
Add `--reject-alpn` to verify that a peer without `h2` is rejected before any
HTTP bytes are sent.
See [the proxy test documentation](h2-proxy.md) for native proxy setup and the
known Envoy downgrade-handshake limitation.
7 changes: 7 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ call `start()` on each instance. Each connection attempt runs on its own backgro
thread, so starting another client does not wait for the first handshake.
`connect()` is the synchronous connection API.

### HTTP/2 client (experimental)

Call `websocket.setHttp2(true)` before connecting to opt into RFC 8441 over
`wss://`. HTTP/1.1 remains the default. This self-contained client opens one H2
stream per connection; it does not share connections between WebSocket objects.
See [HTTP/2 client support](http2.md) for negotiation requirements and testing.

### Threading and callbacks

`setOnMessageCallback()` handles Open, Message, Close, Error, Ping, Pong, and Fragment
Expand Down
Loading
Loading