Skip to content
Merged
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
5 changes: 5 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Read the superproject's [AGENTS.md](../../AGENTS.md) and
every file hash before lint exempts that corpus's upstream style and notices.
Change generated application adaptations through their generator, never by
editing its outputs. Both the adapted sources and their generator are linted.
- `websocket/bounded_handshake_stream.hpp` bounds a client's handshake response
as a next layer, without correcting upstream: native Boost.Beast and the shipped
engine keep upstream's 8 MiB. It parses with Beast's own `basic_parser` and starts
its transport's operations through function pointers, so that misc-no-recursion
sees no cycle through it in a consumer's lint; keep both when changing it.
- TLS shutdown cancellation tests synchronize on the peer's observed close_notify.
Cancelling a lowest layer between two internal socket operations is not proof
that a pending shutdown read was cancelled. WebSocket close already tears down
Expand Down
54 changes: 54 additions & 0 deletions doc/beast.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,52 @@ do not change the compiler configuration or remove upstream Boost overloads; use
`exception-handling=off` for a complete build without exception handling. Prefer
upstream error-code overloads when recovery is required.

[#beast-bounded-handshake]
=== Bound a client's handshake response

A WebSocket client's handshake reads the server's HTTP response with Beast's parser
and its default limits: 8 KiB of header and, for a response that refuses the upgrade,
8 MiB of body, in memory no allocator of the program's reaches. Natively that is
Boost.Beast's own code; on WASI it is the shipped copy of it. A client that connects
to servers it does not trust places `websocket::bounded_handshake_stream` between the
WebSocket stream and its transport, from
`<webcpp/beast/websocket/bounded_handshake_stream.hpp>`, and sets its
`response_limits`:

[source]
----
include::{library-root}/test/handshake_test.cpp[tag=bounded-handshake,indent=0]
----

The limits default to Beast's 8 KiB of header and 1 KiB after the header of a refusal.
The handshake is the stream's own, unchanged:

[source]
----
include::{library-root}/test/handshake_test.cpp[tag=bounded-handshake-call,indent=0]
----

A header longer than its bound fails the handshake with `http::error::header_limit`.
A refusal that declares a longer body fails it with `http::error::body_limit` as soon
as its header is read, before the handshake reserves that body; one that sends more
bytes after its header than the bound fails it with the same error. In all, the layer
reads at most the two bounds of a response. A refusal within them fails with
`websocket::error::upgrade_declined`, its status and body in the response, as upstream
does. A 101 response passes through: every later read and write is the transport's.

Over TLS the TLS stream is one layer further down, `ws.next_layer().next_layer()`, for
its settings and its handshake; teardown after a close is the transport's own. Natively
and on WASI, a WebSocket stream constructed with `std::allocator_arg` and an allocator
passes them on to the layer, which keeps there the rare header line that two reads split.

The plain WebSocket stream keeps upstream's limits: natively it is Boost.Beast's own
type, and the shipped Wasm engine applies no protocol correction. Bounding is a choice
of the client, and the layer is meant for a client: a server's accept reads a request,
which the layer refuses as a malformed response. On Emscripten the browser's WebSocket
reads its own handshake response and exposes none of it to the page. The layer compiles
there for the guest engine of `<webcpp/beast/websocket/guest.hpp>` over a supplied
channel; its runtime tests run natively and on WASIp2/P3 only.

[#beast-explanation]
== Explanation: separate messages from transport

Expand Down Expand Up @@ -246,6 +292,7 @@ API or transport detail of the original program is available.
|Explicit supplied-stream counterpart |Borrowed descriptors |CLI input/output; asynchronous WebSocket via pollables |CLI input/output |Caller-supplied channel
|Raw TCP HTTP/WebSocket listener |Available |Available with network grants |Available |Unavailable
|Host HTTP WebSocket upgrade |Not applicable |Not provided by host adapter |Use guest socket backend |Browser WebSocket client only
|Bounded client handshake response |Bounded next layer |Bounded next layer |Bounded next layer |Browser-owned; compiled for a supplied channel
|Native CPU-parallel threads |Available |Unavailable |Cooperative WASI threads only |Not implied by this backend
|===

Expand Down Expand Up @@ -290,6 +337,13 @@ include::application-matrix.adoc[]
* `socket_test.cpp` exchanges real HTTP and binary WebSocket payloads, completes both
sides of a close handshake, and verifies a stalled peer reaches the stream timeout.
Each case has a five-second outer deadline.
* `handshake_test.cpp` answers a client's upgrade with refusals of a megabyte, declared,
streamed and chunked, and with long 101 headers: the bounded layer fails each with
Beast's limit error after reading at most its bounds, synchronously too, and a
refusal's handshake allocates under 64 KiB. A refusal within the bounds and a 101
read seven bytes at a time, through the layer's allocator, complete as upstream's do.
`test/tls/protocol.cpp` repeats the refusal and an echo whose TLS shutdown passes
through the layer.
* `ssl_test.cpp` checks SNI copying and rejection without replacing an existing valid
name. It does not claim that an SNI setting authenticates the peer.
* `config_test.cpp` checks compiler/global/library exception configuration, both
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Changes to the unreleased Beast facade.
// tag::unreleased[]
* Fixed browser HTTP and WebSocket transfers with shared Wasm memory.
* Added current application coverage and shared Release parser measurements.
* Added a WebSocket client layer that bounds the handshake response it reads.
// end::unreleased[]

[#beast-changelog-0-1-0]
Expand Down
16 changes: 16 additions & 0 deletions include/webcpp/beast/websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,25 @@ class stream : public ::webcpp::beast_backend::websocket::stream<NextLayer, Defl
@note This backend does not claim that the supplied allocator controls upstream state.
*/
template <class Allocator, class... Args>
requires(!std::uses_allocator_v<NextLayer, Allocator>)
explicit stream([[maybe_unused]] std::allocator_arg_t tag,
[[maybe_unused]] const Allocator& allocator, Args&&... args)
: base(std::forward<Args>(args)...) {}

/**
Constructs an allocator-using underlying stream with the allocator, as upstream does natively.

@tparam Allocator The allocator type, which the underlying stream's `allocator_type` takes.
@tparam Args The underlying stream's other constructor argument types.
@param tag Passed on to the underlying stream with the allocator.
@param allocator The underlying stream's allocator, such as a bounded handshake's.
@param args The underlying stream's other constructor arguments.
@note Upstream stream-state allocation is unchanged, as with the other allocator constructor.
*/
template <class Allocator, class... Args>
requires std::uses_allocator_v<NextLayer, Allocator>
explicit stream(std::allocator_arg_t tag, const Allocator& allocator, Args&&... args)
: base(tag, allocator, std::forward<Args>(args)...) {}
};
#endif // WEBCPP_TARGET_NATIVE
} // namespace webcpp::beast::websocket
Expand Down
Loading
Loading