public static class WebSocketUpgradeThe RFC 6455 handshake, as a function of bytes.
No guide page documents this yet — the page shows what the code says about itself.
Remarks
The one part of this transport we parse ourselves, and it runs before anything has authenticated. The framing above it is WebSocket.CreateFromStream — the runtime's, and deliberately not ours. The upgrade is thirty lines of header scanning over bytes from a stranger, which makes it the most exposed code in the package and the reason it is a static function over a span rather than a loop inside an async method: something shaped like this can be fuzzed, and something reading from a NetworkStream cannot.
Bytes rather than a string, which is a bug fix as much as a shape. The previous version decoded and split the whole accumulated buffer on every read, so a client dribbling its request one byte at a time made the server build four thousand strings of up to four kilobytes — about eight megabytes of garbage for four kilobytes sent, at no cost to the sender, times however many sockets they care to open. Scanning the bytes costs nothing and allocates one string, for the key.
Fields and properties (2)
public const string AcceptGuidThe GUID RFC 6455 says to append to the key before hashing it.
public const int MaxRequestBytesThe most of a request that will be read before it is refused.
Methods (3)
public static bool IsComplete(ReadOnlySpan<byte> request, int from, out int length)Whether the headers have finished, and where they finish.
public static bool TryReadKey(ReadOnlySpan<byte> request, out string key)Finds the key a client offered.
public static byte[] Accept(string key)The response a client's key earns.
Used by (3)
- SystemWebSocketListenerVixen.Net.Transport.WebSocket
- WebSocketUpgradeTargetVixen.Net.Fuzz
- WebSocketUpgradeTestsVixen.Net.Transport.WebSocket.Tests