Websocket握手Sec-WebSocket-Accept标头值不正确 [英] Websocket handshake Sec-WebSocket-Accept header value is incorrect

查看:528
本文介绍了Websocket握手Sec-WebSocket-Accept标头值不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个c ++ websocket服务器,chrome上的dev工具说sec-websocket-accept标头值不正确。我已经测试了几天,一切都很好。尽管在chrome dev工具中显示为101,但客户端关闭了readystate 3而没有调用websocket onopen。

I'm writing a c++ websocket server, dev tools on chrome says sec-websocket-accept header value is incorrect. I've tested for days and it all seems fine. The client closes with readystate 3 without the websocket onopen being called although it shows as 101 in chrome dev tools.

这是我计算密钥的代码

string magickey = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
string key = msgkey.append(magickey);

unsigned char* sha_str = SHA1(reinterpret_cast<const unsigned char*>(key.c_str()), key.length(), nullptr);
string acceptkey = base64_encode(reinterpret_cast<const unsigned char*>(sha_str), strlen((char*)sha_str));

string handshake_response = "HTTP/1.1 101 Switching Protocols\r\n";
handshake_response.append("Upgrade: websocket\r\n");
handshake_response.append("Connection: Upgrade\r\n");
handshake_response.append("Sec-WebSocket-Accept: "+acceptkey+"\r\n");
handshake_response.append("\r\n");  

Chrome回复

HTTP / 1.1 101切换协议

升级:websocket

连接:升级

Sec-WebSocket-Accept:5T5MvxP1iz40vLpi3kQs / ifDaCo =

Chrome Response
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: 5T5MvxP1iz40vLpi3kQs/ifDaCo=

Chrome请求

GET ws:// localhost:4897 / echo HTTP / 1.1

主机:localhost:4897

连接:升级

Pragma:否-cache

缓存控制:无缓存

升级:websocket

来源: http:// localhost

Sec-WebSocket-Version:13

User-Agent:Mozilla / 5.0(Windows NT 6.3; WOW64)AppleWebKit / 537.36( KHTML,像Gecko)Chrome / 47.0.2526.106 Safari / 537.36

接受编码:gzip,deflate,sdch

接受语言:en-US,en; q = 0.8 < br>
Sec-WebSocket-Key:LKF8lHGznbKGIgO1UzAOhg ==

Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits

Chrome Request
GET ws://localhost:4897/echo HTTP/1.1
Host: localhost:4897
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://localhost
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Sec-WebSocket-Key: LKF8lHGznbKGIgO1UzAOhg==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

它表示WebSocket握手期间出错:错误'Sec-WebSocket-Accept'标头值。

It says "Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value".

Chrome还会显示一个额外的帧,接收大小为79字节的操作码-1。

Chrome also shows one additional frame received size 79 bytes opcode -1.

谢谢堆!

推荐答案

Chrome说'Sec-WebSocket - 接受'是不正确的。尝试手动计算,我必须同意Chrome。

Chrome says that 'Sec-WebSocket-Accept' is incorrect. Trying to compute it manually, I have to agree with Chrome.

我的测试:


  1. concatLKF8lHGznbKGIgO1UzAOhg ==和258EAFA5-E914-47DA-95CA-C5AB0DC85B11
    =>LKF8lHGznbKGIgO1UzAOhg == 258EAFA5-E914-47DA-95CA-C5AB0DC85B11,即

  2. 计算SHA1 160位十六进制: bf15 14e3 7108 0ee4 7782 c709 a767 cc72 423d e5c4

  3. 从您的日志中,您的编码为base64为:5T5MvxP1iz40vLpi3kQs / ifDaCo =

  4. 将其解码为十六进制: e53e 4cbf 13f5 8b3e 34bc ba62 de44 2cfe
    27c3 682a

  1. concat "LKF8lHGznbKGIgO1UzAOhg==" and "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" => "LKF8lHGznbKGIgO1UzAOhg==258EAFA5-E914-47DA-95CA-C5AB0DC85B11", that's key.
  2. compute SHA1 160 bits hex: bf15 14e3 7108 0ee4 7782 c709 a767 cc72 423d e5c4
  3. From your log, your encoding to base64 is: 5T5MvxP1iz40vLpi3kQs/ifDaCo=
  4. Decoding it to hex: e53e 4cbf 13f5 8b3e 34bc ba62 de44 2cfe 27c3 682a

粗体值应相等。如果我错在某处,请随时纠正我。

The bold values should be equal. Feel free to correct me if I'm wrong somewhere.

可能出现的问题:


  • sha_str 是否以空值终止? ie strlen((char *)sha_str)== 20

  • Is sha_str null-terminated ? i.e. strlen((char*)sha_str) == 20 ?

signed / unsigned char mixup?

signed/unsigned char mixup ?

这篇关于Websocket握手Sec-WebSocket-Accept标头值不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆