MS Http Server API(< Win 8)和WebSockets

时间:2013-05-28 09:35:02

标签: c++ websocket httpserver

我尝试在win server 2008下的MS Http Server API上实现WebSocket协议(没有HTTP_SEND_RESPONSE_FLAG_OPAQUE标志)。

HTTP_RESPONSE response={0};
const char upgrade_val[]="Websocket";
response.Headers.KnownHeaders[HttpHeaderUpgrade].RawValueLength=strlen(upgrade_val);
response.Headers.KnownHeaders[HttpHeaderUpgrade].pRawValue     =upgrade_val;
const char connection_val[]="Upgrade";
response.Headers.KnownHeaders[HttpHeaderConnection].RawValueLength=strlen(connection_val);
response.Headers.KnownHeaders[HttpHeaderConnection].pRawValue     =connection_val;
HTTP_UNKNOWN_HEADER unknown[1];
response.Headers.UnknownHeaderCount=1;
response.Headers.pUnknownHeaders   =unknown;
const char accept_name[]="Sec-WebSocket-Accept";
unknown[0].NameLength    =_countof(accept_name)-1;
unknown[0].pName         =accept_name;
unknown[0].RawValueLength=HANDSHAKE_KEY_LENGTH;
unknown[0].pRawValue     =base64_key;
response.Version.MajorVersion=1;
response.Version.MinorVersion=1;
response.StatusCode          =HTTP_SWITCHING_PROTOCOLS;
const char reason[]              ="Switching Protocols";
response.ReasonLength        =strlen(reason);
response.pReason             =reason;
HttpSendHttpResponse(iocp,RequestId,HTTP_SEND_RESPONSE_FLAG_MORE_DATA,&raw_response,NULL,NULL,NULL,NULL,NULL,NULL);

浏览器发送标题(来自Fiddler)

GET http://server.host/ HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: server.host
Origin: null
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: V86c1TFOwWfZqhS42C0arA==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/27.0.1453.94 Safari/537.36

服务器的回复(来自Fiddler)

HTTP/1.1 101 Switching Protocols
Upgrade: Websocket
Server: Microsoft-HTTPAPI/2.0
Sec-WebSocket-Accept: 3io2SU7uJIeFlwy0+OFJUDNrA44=
Date: Tue, 28 May 2013 09:27:59 GMT
EndTime: 13:27:59.006
ReceivedBytes: 0
SentBytes: 0

和浏览器显示1016错误。

2 个答案:

答案 0 :(得分:1)

假设您的Sec-WebSocket-Accept标头值已正确计算,并假设您的response变量应该被命名为raw_response,那么您的响应将缺少必需的Connection: Upgrade标头尽管你的代码为它分配了一个值。

答案 1 :(得分:0)

WebSockets需要

HTTP_SEND_RESPONSE_FLAG_OPAQUE(Windows 8及更高版本)。

WebSocket servers on Windows Server(2011-12-20)

  

目前,Server 2008 / R2盒子无法托管WebSockets。   至少,不是在与IIS Web服务器共享端口80和443时

Re: [hybi] WebSocket protocol as it stands(2010-11-06)

  

我们需要调整http.sys来识别真正的非HTTP   请求,作为HTTP请求