aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_websocket.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-09-15 22:03:00 +0200
committerLoïc Hoguin <[email protected]>2012-09-15 22:03:00 +0200
commit27d591180ca3dd8b0d3c63c1293da5a3c4f4321f (patch)
tree271f0e533b210799c442ff28542146e56b8b15ee /src/cowboy_websocket.erl
parentcd54214deff03d5ed4a487f9976c3db14f579961 (diff)
downloadcowboy-27d591180ca3dd8b0d3c63c1293da5a3c4f4321f.tar.gz
cowboy-27d591180ca3dd8b0d3c63c1293da5a3c4f4321f.tar.bz2
cowboy-27d591180ca3dd8b0d3c63c1293da5a3c4f4321f.zip
Add cowboy_req:url/1 to return the full request URL
Use it in cowboy_websocket for hixie76, replacing http by ws.
Diffstat (limited to 'src/cowboy_websocket.erl')
-rw-r--r--src/cowboy_websocket.erl56
1 files changed, 4 insertions, 52 deletions
diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl
index 04082e1..2db0faa 100644
--- a/src/cowboy_websocket.erl
+++ b/src/cowboy_websocket.erl
@@ -26,7 +26,6 @@
-export([handler_loop/4]).
-include("http.hrl").
--include_lib("eunit/include/eunit.hrl").
-type opcode() :: 0 | 1 | 2 | 8 | 9 | 10.
-type mask_key() :: 0..16#ffffffff.
@@ -164,15 +163,14 @@ upgrade_denied(#http_req{socket=Socket, transport=Transport,
-spec websocket_handshake(#state{}, cowboy_req:req(), any()) -> closed.
websocket_handshake(State=#state{socket=Socket, transport=Transport,
version=0, origin=Origin, challenge={Key1, Key2}},
- Req=#http_req{host=Host, port=Port, path=Path, raw_qs=QS},
- HandlerState) ->
- Location = hixie76_location(Transport:name(), Host, Port, Path, QS),
+ Req, HandlerState) ->
+ {<< "http", Location/binary >>, Req1} = cowboy_req:url(Req),
{ok, Req2} = cowboy_req:upgrade_reply(
<<"101 WebSocket Protocol Handshake">>,
[{<<"Upgrade">>, <<"WebSocket">>},
- {<<"Sec-Websocket-Location">>, Location},
+ {<<"Sec-Websocket-Location">>, << "ws", Location/binary >>},
{<<"Sec-Websocket-Origin">>, Origin}],
- Req),
+ Req1),
%% Flush the resp_sent message before moving on.
receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
%% We replied with a proper response. Proxies should be happy enough,
@@ -554,29 +552,6 @@ hixie76_key_to_integer(Key) ->
Spaces = length([C || << C >> <= Key, C =:= 32]),
Number div Spaces.
--spec hixie76_location(atom(), binary(), inet:port_number(),
- binary(), binary()) -> binary().
-hixie76_location(Protocol, Host, Port, Path, <<>>) ->
- << (hixie76_location_protocol(Protocol))/binary, "://", Host/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(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:port_number()) -> binary().
-hixie76_location_port(ssl, 443) ->
- <<>>;
-hixie76_location_port(tcp, 80) ->
- <<>>;
-hixie76_location_port(_, Port) ->
- <<":", (list_to_binary(integer_to_list(Port)))/binary>>.
-
%% hybi specific.
-spec hybi_challenge(binary()) -> binary().
@@ -592,26 +567,3 @@ hybi_payload_length(N) ->
N when N =< 16#ffff -> << 126:7, N:16 >>;
N when N =< 16#7fffffffffffffff -> << 127:7, N:64 >>
end.
-
-%% Tests.
-
--ifdef(TEST).
-
-hixie76_location_test() ->
- ?assertEqual(<<"ws://localhost/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(tcp, <<"localhost">>, 8080, <<"/path">>, <<>>)),
- ?assertEqual(<<"ws://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">>,
- hixie76_location(ssl, <<"localhost">>, 8443, <<"/path">>, <<>>)),
- ?assertEqual(<<"wss://localhost:8443/path?dummy=2785">>,
- hixie76_location(ssl, <<"localhost">>, 8443, <<"/path">>, <<"dummy=2785">>)),
- ok.
-
--endif.