पाठ 10 / 32

The Opening Handshake

यह पाठ अभी हिंदी में अनुवादित नहीं है, इसलिए अंग्रेज़ी में दिखाया जा रहा है।

Walk through the HTTP Upgrade handshake and compute Sec-WebSocket-Accept by hand.

लेखक: · 15 साल से वेब डेवलपर

From HTTP request to WebSocket

A WebSocket (RFC 6455) starts life as an ordinary HTTP/1.1 GET with extra headers: Connection: Upgrade, Upgrade: websocket, Sec-WebSocket-Version: 13 and Sec-WebSocket-Key, a random 16-byte value in base64. Browsers also send Origin, and may send Sec-WebSocket-Protocol to propose subprotocols such as graphql-transport-ws. If the server agrees, it replies 101 Switching Protocols with Sec-WebSocket-Accept: the base64 SHA-1 of the client key concatenated with a fixed GUID from the RFC. This proves the server really speaks WebSocket and is not a confused HTTP cache replaying a response; it is not authentication or encryption. After the 101, the same TCP connection stops carrying HTTP and carries WebSocket frames in both directions. ws:// runs over plain TCP (port 80 by default) and wss:// over TLS (port 443); always use wss:// in production.

The life of one WebSocket connection

Every WebSocket goes through the same four phases. Only the first is HTTP; everything after the 101 is the WebSocket framing protocol.

Four connected stages in order: HTTP Upgrade request, 101 Switching Protocols response, data frames in both directions, closing handshake.
Figure 4.1: Upgrade request -> 101 Switching Protocols -> frames both ways -> close handshake

Computing Sec-WebSocket-Accept

The key below is the example from RFC 6455; the printed value matches the RFC. Every server library does exactly this.

import base64
import hashlib

GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

def accept_key(sec_websocket_key: str) -> str:
    digest = hashlib.sha1((sec_websocket_key + GUID).encode()).digest()
    return base64.b64encode(digest).decode()

print(accept_key("dGhlIHNhbXBsZSBub25jZQ=="))

Output:

s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

Watching a real handshake with curl

curl can send the upgrade headers itself. Against a local ws server, the reply is the 101 response with the same accept value computed above.

curl -i -N \
  -H "Connection: Upgrade" \
  -H "Upgrade: websocket" \
  -H "Sec-WebSocket-Version: 13" \
  -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" \
  http://localhost:8080/

Output:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

Exam favourite: what the key is for

Students often say Sec-WebSocket-Key secures the connection. It does not. It only lets the client check that the server understood the WebSocket handshake. Security comes from wss:// (TLS) and from your own authentication and Origin checks.

त्वरित जाँच: Which HTTP status code does a server return to accept a WebSocket upgrade?

  • 200 OK
  • 204 No Content
  • 302 Found
  • 101 Switching Protocols
Answer

101 Switching Protocols — 101 tells the client the connection is switching from HTTP to the protocol named in the Upgrade header.