yoyo-yuan
yoyo-yuan

Reputation: 71

websocket handshake

I'm writing a websocket server using c++,I was pending in the handshake use chrome 17 as the client.

When the server send client the handshake response chrome always show the error below in console:

Error during WebSocket handshake: Sec-WebSocket-Accept mismatch

The event in chrome is as below:

t=1328796971951 [st= 5]    WEB_SOCKET_SEND_REQUEST_HEADERS  
--> GET / HTTP/1.1   
Upgrade: websocket
Connection: Upgrade
Host: 127.0.0.1:38950
Origin: null     
Sec-WebSocket-Key: zMb+UCeRb+2OmMp9fpbxHw==
Sec-WebSocket-Version: 13

t=1328796971951 [st= 5]    SOCKET_STREAM_SENT     
t=1328796971971 [st=25]    SOCKET_STREAM_RECEIVED  
t=1328796971971 [st=25]    WEB_SOCKET_READ_RESPONSE_HEADERS  
--> HTTP/1.1 101 Switching Protocols
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Accept: 4emBYsdkl0SxeUMGLNc0dFsI1/E=


t=1328796971972 [st=26] -REQUEST_ALIVE

Please help.

Upvotes: 1

Views: 2640

Answers (1)

idoodler
idoodler

Reputation: 3545

The Sec-WebSocket-Accept value is not correctly calculated.

How to calculate the value (pseudocode):

// Getting the Sec-WebSocket-Key from the Request header
var sec_Websocket_Key = requestHeader["Sec-WebSocket-Key"];
// Adding the magic string to sec_Websocket_key
// sha1 hash this new value
var sec_Websocket_Key_Hash = (sec_Websocket-Key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").sha1();
// Get the Base64 String of the hash
var sec_Websocket_Accept = sec_Websocket_Key_Hash.toBase64String();
// sec_Websocket_Accept now is the correct value to set in the Header

Get more informations here

Upvotes: 2

Related Questions