Master the fundamental concepts of protocol implementation through this focused micro-challenge.
WebSockets start as HTTP/1.1 on TCP port 80 or 443, then switch to a binary framing protocol. The client sends:
cLoading…
The server replies 101 Switching Protocols with Sec-WebSocket-Accept set to Base64(SHA1(key + 258EAFA5-E914-47DA-95CA-C5AB0DC85B11)).
After the handshake, messages use 2-14 byte headers plus payload:
1 = text, 2 = binary, 8 = close, 9 = ping, 10 = pongPayload length uses a compact encoding: values under 126 fit in 7 bits, 126 means the next 2 bytes hold the length, and 127 means the next 8 bytes hold it. This lets a single text frame carry a short chat message or a large binary blob over the same TCP connection.
This task asks you to document the handshake and parse a WebSocket frame. RFC 6455 mandates client-side masking specifically to prevent cache-poisoning attacks against misbehaving HTTP proxies. nginx's proxy_pass WebSocket support and every browser's WebSocket API perform this same Sec-WebSocket-Accept SHA-1 handshake before switching protocols on port 443 or 80.
Write a C program that documents the WebSocket handshake and parses a WebSocket frame. Show key generation, accept calculation, and frame decode.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github