aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-06-27 07:06:34 -0700
committerLoïc Hoguin <[email protected]>2011-06-27 07:06:34 -0700
commit227529761c835637a5dbf77c83160d62a71ec720 (patch)
tree31030dbaeb3ec0e835404ad95a9a8f1e770cead4 /src
parentf03dbf4d0662a231454f7c188c10e2892c2733ce (diff)
parentbebe3dc5d2012877cdaf349012517eed40a93112 (diff)
downloadcowboy-227529761c835637a5dbf77c83160d62a71ec720.tar.gz
cowboy-227529761c835637a5dbf77c83160d62a71ec720.tar.bz2
cowboy-227529761c835637a5dbf77c83160d62a71ec720.zip
Merge pull request #29 from smarkets/ws-default-ports
do not send ports 80 and 443 - browsers get mad
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_http_websocket.erl33
1 files changed, 27 insertions, 6 deletions
diff --git a/src/cowboy_http_websocket.erl b/src/cowboy_http_websocket.erl
index 6a972da..8463ff5 100644
--- a/src/cowboy_http_websocket.erl
+++ b/src/cowboy_http_websocket.erl
@@ -17,6 +17,7 @@
-export([handler_loop/4]). %% Internal.
-include("include/http.hrl").
+-include_lib("eunit/include/eunit.hrl").
-record(state, {
handler :: module(),
@@ -102,12 +103,18 @@ websocket_handshake(State=#state{origin=Origin, challenge=Challenge},
-spec websocket_location(atom(), binary(), inet:ip_port(), binary())
-> binary().
-websocket_location(ssl, Host, Port, Path) ->
- << "wss://", Host/binary, ":",
- (list_to_binary(integer_to_list(Port)))/binary, Path/binary >>;
-websocket_location(_Any, Host, Port, Path) ->
- << "ws://", Host/binary, ":",
- (list_to_binary(integer_to_list(Port)))/binary, Path/binary >>.
+websocket_location(Protocol, Host, Port, Path) ->
+ << (websocket_location_protocol(Protocol))/binary, "://", Host/binary,
+ (websocket_location_port(ssl, Port))/binary, Path/binary >>.
+
+-spec websocket_location_protocol(atom()) -> binary().
+websocket_location_protocol(ssl) -> <<"wss">>;
+websocket_location_protocol(_) -> <<"ws">>.
+
+-spec websocket_location_port(atom(), inet:ip_port()) -> binary().
+websocket_location_port(ssl, 443) -> <<"">>;
+websocket_location_port(_, 80) -> <<"">>;
+websocket_location_port(_, Port) -> <<":", (list_to_binary(integer_to_list(Port)))/binary>>.
-spec handler_before_loop(#state{}, #http_req{}, any(), binary()) -> ok.
handler_before_loop(State=#state{hibernate=true},
@@ -218,3 +225,17 @@ handler_terminate(#state{handler=Handler, opts=Opts},
[Handler, Class, Reason, TerminateReason, Opts,
HandlerState, Req, erlang:get_stacktrace()])
end.
+
+%% eunit
+
+-ifdef(TEST).
+
+websocket_location_test() ->
+ ?assertEqual(<<"ws://localhost/path">>, websocket_location(other, <<"localhost">>, 80, <<"/path">>)),
+ ?assertEqual(<<"ws://localhost:8080/path">>, websocket_location(other, <<"localhost">>, 8080, <<"/path">>)),
+ ?assertEqual(<<"wss://localhost/path">>, websocket_location(ssl, <<"localhost">>, 443, <<"/path">>)),
+ ?assertEqual(<<"wss://localhost:8443/path">>, websocket_location(ssl, <<"localhost">>, 8443, <<"/path">>)),
+ ok.
+
+-endif.
+