Function web_socket::handshake::response
source · pub fn response(
sec_ws_key: impl AsRef<str>,
headers: impl IntoIterator<Item = impl Header>
) -> StringExpand description
Server handshake response
When the server receives the handshake request, It should send back a special response that indicates that the protocol will be changing from HTTP to WebSocket.
The Sec-WebSocket-Accept header is important in that the server must derive it from the Sec-WebSocket-Key that the client sent to it.
Example
let res = [
"HTTP/1.1 101 Switching Protocols",
"Upgrade: websocket",
"Connection: Upgrade",
"Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
"",
""
];
let field: Option<(&str, &str)> = None;
assert_eq!(web_socket::handshake::response("dGhlIHNhbXBsZSBub25jZQ==", field), res.join("\r\n"));To get it, concatenate the client’s Sec-WebSocket-Key and the string “258EAFA5-E914-47DA-95CA-C5AB0DC85B11” together (it’s a Magic string), take the SHA-1 hash of the result, and return the base64 encoding of that hash.
-
If the connection is happening on an HTTPS (HTTP-over-TLS) port, perform a TLS handshake over the connection. If this fails (e.g., the client indicated a host name in the extended client hello “server_name” extension that the server does not host), then close the connection.
-
The server can perform additional client authentication, Or The server MAY redirect the client.
Note
- Regular HTTP status codes can be used only before the handshake. After the handshake succeeds, you have to use a different set of codes (defined in section 7.4 of the spec)