some of you may know i've been running truce's torque webserver.
i've modified it a bit to allow support for various things, and my latest goal is to add support for html5 websockets.
following this guide and numerous other resources
http://www.altdevblogaday.com/2012/01/23/writing-your-own-websocket-server/and
https://developer.mozilla.org/en-US/docs/WebSocketsi've made this addition to the
Webclient::finish method in the webserver at line 253
if($_SERVERHTTP_Connection $= "Upgrade")
{
%server.debug("> Upgrading connection to websocket.");
if($WebSocketNum $= "")
$WebSocketNum = 0;
%spec = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
%clkey = $_SERVERHTTP_Sec_WebSocket_Key;
%catkey = %clkey @ %spec;
%server.debug("> CatKey: " @ %catkey);
%rkey = base64Encode(sha1(%catkey));
%server.debug("> key: " @ %rkey);
%this.send("HTTP/1.1 101 Switching Protocols\r\n");
%this.send("Upgrade: websocket\r\n");
%this.send("Connection: Upgrade\r\n");
%this.send("Sec-WebSocket-Accept: " @ %rkey @ "\r\n");
%this.send("\r\n");
// %this.send("Num = " @ $WebSocketNum @ "\r\n");
$WebSocketArray[$WebSocketNum] = %this;
$WebSocketNum++;
return;
}
the problem occurs when the system is calculating either the SHA1 sum or the base64 encoding at the end. here's a sample request utilizing javascript console:
var s = new WebSocket("69.64.43.11:33580");
% [Webserver] > Client timeout in 500 milliseconds.
% [Webserver] Packet terminated from client 1845687.
% [Webserver] Parsing client 1845687's header: Upgrade: websocket
Connection: Upgrade
Host: 69.64.43.11:33580
Origin: http://69.64.43.11:33580
Sec-WebSocket-Key: aGf7b7cHpm/HtRf16dG3Xg==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
Cookie: a=streamline; n=Lugnut
% [Webserver] > Assigning websocket to HTTP_Upgrade.
% [Webserver] > Assigning Upgrade to HTTP_Connection.
% [Webserver] > Assigning 69.64.43.11:33580 to HTTP_Host.
% [Webserver] > Assigning http://69.64.43.11:33580 to HTTP_Origin.
% [Webserver] > Assigning aGf7b7cHpm/HtRf16dG3Xg== to HTTP_Sec_WebSocket_Key.
% [Webserver] > Assigning 13 to HTTP_Sec_WebSocket_Version.
% [Webserver] > Assigning x-webkit-deflate-frame to HTTP_Sec_WebSocket_Extensions.
% [Webserver] > Assigning a=streamline; n=Lugnut to HTTP_Cookie.
% [Webserver] Parsing client 1845687's POST args:
% [Webserver] > No POST args found to parse!
% [Webserver] > Upgrading connection to websocket.
% [Webserver] > clKey = aGf7b7cHpm/HtRf16dG3Xg==
% [Webserver] > recdKey = aGf7b7cHpm/HtRf16dG3Xg==
% [Webserver] > CatKey: aGf7b7cHpm/HtRf16dG3Xg==258EAFA5-E914-47DA-95CA-C5AB0DC85B11
% [Webserver] > key: ZDdkN2FlMGM5MzFmN2RhY2Y0NDRlOWJmNzVmZTJjNzBkNTQyNzIwOQ==
Error during WebSocket handshake: Sec-WebSocket-Accept mismatch
as you can see, the specification magic key is appended to the sent key successfully, and the end result is pooped out.
using default programs in my linux distro, i ran the following commands in my terminal
lugnut@LugBook:~$ sha1sum
aGf7b7cHpm/HtRf16dG3Xg==258EAFA5-E914-47DA-95CA-C5AB0DC85B11
bf416c18230328f3113518ed03f8fbcee351d7b4 -
lugnut@LugBook:~$ base64
bf416c18230328f3113518ed03f8fbcee351d7b4
YmY0MTZjMTgyMzAzMjhmMzExMzUxOGVkMDNmOGZiY2VlMzUxZDdiNAo=
firstly i input the whole key (sent + spec) into sha1sum
then i took that and put it into the base64 converter
obviously the webservers result is drastically different than the one i manually created.
however, i don't know who to blame :(
it appears that torque is miscalculating either base64 or sha1...
What can I do?
EDIT: after posting this i realized there was two spaces and a - after my sha1 encoding i had ignored
lugnut@LugBook:~$ base64
bf416c18230328f3113518ed03f8fbcee351d7b4 -
YmY0MTZjMTgyMzAzMjhmMzExMzUxOGVkMDNmOGZiY2VlMzUxZDdiNCAgLQo=
this is a match to my earlier result without the whitespace.