[{"id":"websocketsintermediate-001","courseId":"websockets-intermediate","domain":"opening-handshake","topic":"Sec-WebSocket-Accept value","prompt":"A client's opening request carries a random Sec-WebSocket-Key, and the server accepts the upgrade. Which value must the server return in the Sec-WebSocket-Accept header?","hint":"The client must be able to verify the reply. What could the server compute from what the client sent?","explain":"A handshake reply must prove the server read the client's random key, and the client must be able to check that proof itself. Both sides share one fixed constant from the spec and a standard one-way transformation.","source":"https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers","interaction":{"type":"single"},"options":[{"id":"a","text":"The Sec-WebSocket-Key value copied back unchanged","whyNot":"The server must derive this header from the key, not echo the key itself."},{"id":"b","text":"Base64 of the SHA-1 hash of the key with a fixed magic string appended","why":"The server appends 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 to the key, takes the SHA-1 hash, and base64-encodes the result."},{"id":"c","text":"A new random 16-byte value the server generates for this reply","whyNot":"The value is derived from the client's key, which is what lets the client verify it."},{"id":"d","text":"The SHA-1 hash of the key alone, with nothing appended","whyNot":"The fixed magic string must be concatenated with the key before hashing."}],"answer":"b","explanation":"The server builds Sec-WebSocket-Accept from the client's key: it appends the fixed GUID, takes the SHA-1 hash, and base64-encodes the result.","concept":"The server builds Sec-WebSocket-Accept from the client's key: it appends the fixed GUID, takes the SHA-1 hash, and base64-encodes the result.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Sec-WebSocket-Accept is derived from the key, so a server that just echoes the key fails the check."]},{"id":"websocketsintermediate-002","courseId":"websockets-intermediate","domain":"opening-handshake","topic":"Sec-WebSocket-Version header","prompt":"Under RFC 6455, a client that sends Sec-WebSocket-Version: 12 in its opening handshake still follows the spec.","hint":"Look for the exact value the spec requires the client to send in this header.","explain":"Each handshake header records a detail the two ends must match, such as which numbered revision of the protocol is in use. A server that does not know the revision a client asked for has a defined way to say so.","source":"https://www.rfc-editor.org/rfc/rfc6455.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"The spec fixes this header at 13; a server lists the versions it understands only when it cannot handle the one requested."},{"id":"b","text":"False","why":"The spec says the value MUST be 13, so a client sending 12 breaks that requirement."}],"answer":"b","explanation":"RFC 6455 requires a client's Sec-WebSocket-Version to be 13. A server that lacks the requested version replies with the versions it understands.","concept":"RFC 6455 requires a client's Sec-WebSocket-Version to be 13. A server that lacks the requested version replies with the versions it understands.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Send Sec-WebSocket-Version: 13; a server that lacks that version answers with the versions it understands."]},{"id":"websocketsintermediate-003","courseId":"websockets-intermediate","domain":"opening-handshake","topic":"Non-101 response to an upgrade","prompt":"A WebSocket client sends a valid upgrade request. The server replies 200 OK with a correct Sec-WebSocket-Accept header and no other problems. What does the client do?","hint":"Check which status code the upgrade needs before the switch can happen.","explain":"A connection can switch protocols partway through, but only after both sides agree to it. That agreement is stated in the status line; any other status falls under the general rules of the underlying protocol.","source":"https://www.rfc-editor.org/rfc/rfc6455.html","interaction":{"type":"single"},"options":[{"id":"a","text":"Opens the WebSocket, since the accept value matches","whyNot":"The accept header only matters once the status confirms the switch, and a 200 response does not."},{"id":"b","text":"Treats the reply as an ordinary HTTP response; no WebSocket opens","why":"Only a 101 status confirms the protocol switch; any other code means the client handles the reply per HTTP and the connection is not established."},{"id":"c","text":"Resends the request with a new Sec-WebSocket-Key","whyNot":"The key did not cause the failure; the status code did, so a fresh key changes nothing."},{"id":"d","text":"Fails only if the Sec-WebSocket-Accept header is missing","whyNot":"A correct accept header does not turn a non-101 reply into a protocol switch."}],"answer":"b","explanation":"A WebSocket upgrade needs a 101 Switching Protocols status. Any other status is handled as plain HTTP, and the WebSocket connection is not established.","concept":"A WebSocket upgrade needs a 101 Switching Protocols status. Any other status is handled as plain HTTP, and the WebSocket connection is not established.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Check the status code first: a correct Sec-WebSocket-Accept on a 200 response opens nothing."]},{"id":"websocketsintermediate-004","courseId":"websockets-intermediate","domain":"opening-handshake","topic":"Origin header on the handshake","prompt":"A chat server accepts WebSocket connections from many websites. Which handshake header tells it which site's page opened a browser's connection?","hint":"Think about a header a browser adds on its own, not one the page chooses.","explain":"Web pages from many sites can open connections to one server, so the server must learn where each request came from. Browsers add a field that records this.","source":"https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers","interaction":{"type":"single"},"options":[{"id":"a","text":"Host","whyNot":"Host names the server the request is addressed to, not the page that opened the connection."},{"id":"b","text":"Origin","why":"Browsers send an Origin header on the handshake, letting the server check which site the request came from."},{"id":"c","text":"Upgrade","whyNot":"Upgrade names the protocol to switch to, here websocket, not the site that asked."},{"id":"d","text":"Sec-WebSocket-Protocol","whyNot":"That header lists the subprotocols the client wants, not where the page came from."}],"answer":"b","explanation":"Browsers send an Origin header on the WebSocket handshake, and the RFC requires it for browser clients, so a server can check which site opened the connection.","concept":"Browsers send an Origin header on the WebSocket handshake, and the RFC requires it for browser clients, so a server can check which site opened the connection.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Check the Origin header on each handshake; browsers always send it, but non-browser clients can fake it."]},{"id":"websocketsintermediate-005","courseId":"websockets-intermediate","domain":"opening-handshake","topic":"Required fields in the opening request","prompt":"A browser opens a WebSocket to a server. Which TWO statements about its opening request are required by the protocol?","hint":"Separate what the client must send from what the server answers with, or sends only when a feature is wanted.","explain":"An opening request for a live protocol has a few mandatory fields, plus optional ones that appear only when the client wants an extra feature. Some fields belong to the other side's reply and never appear in the request.","source":"https://www.rfc-editor.org/rfc/rfc6455.html","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"It uses the GET method over HTTP/1.1 or a later version","why":"The spec says the request method MUST be GET and the HTTP version MUST be at least 1.1."},{"id":"b","text":"It carries a Sec-WebSocket-Key header holding a randomly selected 16-byte value","why":"The client must send a nonce in this header: a randomly selected 16-byte value, base64-encoded."},{"id":"c","text":"It uses the POST method, with the upgrade details in the body","whyNot":"The method MUST be GET, so a POST cannot be the opening request."},{"id":"d","text":"It carries a Sec-WebSocket-Accept header computed from its own key","whyNot":"Sec-WebSocket-Accept is the server's reply; the client sends only the key."},{"id":"e","text":"Its Connection header says keep-alive and nothing else","whyNot":"The Connection header MUST include the Upgrade token, so keep-alive alone is not enough."},{"id":"f","text":"It always includes a Sec-WebSocket-Protocol header","whyNot":"That header is optional; it appears only when the client asks for a subprotocol."}],"answer":"ab","explanation":"The opening request is a GET over HTTP/1.1 or later, with Upgrade: websocket, a Connection header including Upgrade, a random 16-byte key, Host, and Sec-WebSocket-Version 13.","concept":"The opening request is a GET over HTTP/1.1 or later, with Upgrade: websocket, a Connection header including Upgrade, a random 16-byte key, Host, and Sec-WebSocket-Version 13.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["The client's opening request is a GET with a random key; the accept value belongs to the server's reply."]}]