25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-02 15:27:09 +01:00

Make Safari happy use value from Host in WebSocket-Location header

Safari aborts connection if WebSocket-Location contains port number when
Host didn't have it, this change uses value sent by browser directly
instead of making in manually.
This commit is contained in:
Paweł Chmielowski 2012-03-13 14:44:09 +01:00
parent cf6d67ceed
commit 431c34709c

View File

@ -188,6 +188,10 @@ handshake({'draft-hixie', 0}, Sock,SocketMod, Headers, {Path, Q,Origin, Host, Po
% build data % build data
{_, Key1} = lists:keyfind("Sec-Websocket-Key1",1, Headers), {_, Key1} = lists:keyfind("Sec-Websocket-Key1",1, Headers),
{_, Key2} = lists:keyfind("Sec-Websocket-Key2",1, Headers), {_, Key2} = lists:keyfind("Sec-Websocket-Key2",1, Headers),
HostPort = case lists:keyfind('Host', 1, Headers) of
{_, Value} -> Value;
_ -> string:join([Host, integer_to_list(Port)],":")
end,
% handshake needs body of the request, still need to read it [TODO: default recv timeout hard set, will be exported when WS protocol is final] % handshake needs body of the request, still need to read it [TODO: default recv timeout hard set, will be exported when WS protocol is final]
case SocketMod of case SocketMod of
gen_tcp -> gen_tcp ->
@ -220,25 +224,24 @@ handshake({'draft-hixie', 0}, Sock,SocketMod, Headers, {Path, Q,Origin, Host, Po
"Upgrade: WebSocket\r\n", "Upgrade: WebSocket\r\n",
"Connection: Upgrade\r\n", "Connection: Upgrade\r\n",
"Sec-WebSocket-Origin: ", Origin, "\r\n", "Sec-WebSocket-Origin: ", Origin, "\r\n",
"Sec-WebSocket-Location: ws://", "Sec-WebSocket-Location: ws://", HostPort, "/", string:join(Path,"/"),
string:join([Host, integer_to_list(Port)],":"), QString, "\r\n\r\n",
"/",string:join(Path,"/"),QString, "\r\n\r\n",
build_challenge({'draft-hixie', 0}, {Key1, Key2, Body}) build_challenge({'draft-hixie', 0}, {Key1, Key2, Body})
]; ];
handshake({'draft-hixie', 68}, _Sock,_SocketMod, _Headers, {Path, Origin, Host, Port}) -> handshake({'draft-hixie', 68}, _Sock,_SocketMod, Headers, {Path, Origin, Host, Port}) ->
% prepare handhsake response HostPort = case lists:keyfind('Host', 1, Headers) of
{_, Value} -> Value;
_ -> string:join([Host, integer_to_list(Port)],":")
end,
["HTTP/1.1 101 Web Socket Protocol Handshake\r\n", ["HTTP/1.1 101 Web Socket Protocol Handshake\r\n",
"Upgrade: WebSocket\r\n", "Upgrade: WebSocket\r\n",
"Connection: Upgrade\r\n", "Connection: Upgrade\r\n",
"WebSocket-Origin: ", Origin , "\r\n", "WebSocket-Origin: ", Origin , "\r\n",
"WebSocket-Location: ws://", "WebSocket-Location: ws://", HostPort, "/", string:join(Path,"/"),"\r\n\r\n"
lists:concat([Host, integer_to_list(Port)]),
"/",string:join(Path,"/"), "\r\n\r\n"
]; ];
handshake({'draft-hybi', _}, Sock,SocketMod, Headers, {Path, Q,Origin, Host, Port}) -> handshake({'draft-hybi', _}, Sock,SocketMod, Headers, {Path, Q,Origin, Host, Port}) ->
% build data
{_, Key} = lists:keyfind("Sec-Websocket-Key",1, Headers), {_, Key} = lists:keyfind("Sec-Websocket-Key",1, Headers),
Hash = jlib:encode_base64(binary_to_list(sha:sha1(Key++"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))), Hash = jlib:encode_base64(binary_to_list(sha:sha1(Key++"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))),
["HTTP/1.1 101 Switching Protocols\r\n", ["HTTP/1.1 101 Switching Protocols\r\n",
"Upgrade: websocket\r\n", "Upgrade: websocket\r\n",
"Connection: Upgrade\r\n", "Connection: Upgrade\r\n",