diff options
Diffstat (limited to 'src/cowboy_http_websocket.erl')
-rw-r--r-- | src/cowboy_http_websocket.erl | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/src/cowboy_http_websocket.erl b/src/cowboy_http_websocket.erl index e481d4b..61917b4 100644 --- a/src/cowboy_http_websocket.erl +++ b/src/cowboy_http_websocket.erl @@ -76,10 +76,9 @@ upgrade(ListenerPid, Handler, Opts, Req) -> %% instead of having ugly code like this case here. -spec websocket_upgrade(#state{}, #http_req{}) -> {ok, #state{}, #http_req{}}. websocket_upgrade(State, Req) -> - case cowboy_http_req:header('Connection', Req) of - {<<"Upgrade">>, Req2} -> ok; - {<<"keep-alive, Upgrade">>, Req2} -> ok %% @todo Temp. For Firefox 6. - end, + {tokens, ConnTokens, Req2} + = cowboy_http_req:parse_header('Connection', Req), + true = lists:member(<<"Upgrade">>, ConnTokens), {Version, Req3} = cowboy_http_req:header(<<"Sec-Websocket-Version">>, Req2), websocket_upgrade(Version, State, Req3). @@ -364,7 +363,7 @@ handler_call(State=#state{handler=Handler, opts=Opts}, Req, HandlerState, %% hixie-76 text frame. websocket_send({text, Payload}, #state{version=0}, #http_req{socket=Socket, transport=Transport}) -> - Transport:send(Socket, << 0, Payload/binary, 255 >>); + Transport:send(Socket, [0, Payload, 255]); %% Ignore all unknown frame types for compatibility with hixie 76. websocket_send(_Any, #state{version=0}, _Req) -> ignore; @@ -376,9 +375,9 @@ websocket_send({Type, Payload}, _State, ping -> 9; pong -> 10 end, - Len = hybi_payload_length(byte_size(Payload)), - Transport:send(Socket, << 1:1, 0:3, Opcode:4, - 0:1, Len/bits, Payload/binary >>). + Len = hybi_payload_length(iolist_size(Payload)), + Transport:send(Socket, [<< 1:1, 0:3, Opcode:4, 0:1, Len/bits >>, + Payload]). -spec websocket_close(#state{}, #http_req{}, any(), {atom(), atom()}) -> ok. websocket_close(State=#state{version=0}, Req=#http_req{socket=Socket, @@ -427,19 +426,21 @@ hixie76_key_to_integer(Key) -> -> binary(). hixie76_location(Protocol, Host, Port, Path, <<>>) -> << (hixie76_location_protocol(Protocol))/binary, "://", Host/binary, - (hixie76_location_port(ssl, Port))/binary, Path/binary>>; + (hixie76_location_port(Protocol, Port))/binary, Path/binary>>; hixie76_location(Protocol, Host, Port, Path, QS) -> << (hixie76_location_protocol(Protocol))/binary, "://", Host/binary, - (hixie76_location_port(ssl, Port))/binary, Path/binary, "?", QS/binary >>. + (hixie76_location_port(Protocol, Port))/binary, Path/binary, "?", QS/binary >>. -spec hixie76_location_protocol(atom()) -> binary(). hixie76_location_protocol(ssl) -> <<"wss">>; hixie76_location_protocol(_) -> <<"ws">>. +%% @todo We should add a secure/0 function to transports +%% instead of relying on their name. -spec hixie76_location_port(atom(), inet:ip_port()) -> binary(). hixie76_location_port(ssl, 443) -> <<>>; -hixie76_location_port(_, 80) -> +hixie76_location_port(tcp, 80) -> <<>>; hixie76_location_port(_, Port) -> <<":", (list_to_binary(integer_to_list(Port)))/binary>>. @@ -466,11 +467,13 @@ hybi_payload_length(N) -> hixie76_location_test() -> ?assertEqual(<<"ws://localhost/path">>, - hixie76_location(other, <<"localhost">>, 80, <<"/path">>, <<>>)), + hixie76_location(tcp, <<"localhost">>, 80, <<"/path">>, <<>>)), + ?assertEqual(<<"ws://localhost:443/path">>, + hixie76_location(tcp, <<"localhost">>, 443, <<"/path">>, <<>>)), ?assertEqual(<<"ws://localhost:8080/path">>, - hixie76_location(other, <<"localhost">>, 8080, <<"/path">>, <<>>)), + hixie76_location(tcp, <<"localhost">>, 8080, <<"/path">>, <<>>)), ?assertEqual(<<"ws://localhost:8080/path?dummy=2785">>, - hixie76_location(other, <<"localhost">>, 8080, <<"/path">>, <<"dummy=2785">>)), + hixie76_location(tcp, <<"localhost">>, 8080, <<"/path">>, <<"dummy=2785">>)), ?assertEqual(<<"wss://localhost/path">>, hixie76_location(ssl, <<"localhost">>, 443, <<"/path">>, <<>>)), ?assertEqual(<<"wss://localhost:8443/path">>, |