mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
do not send ports 80 and 443 - browsers get mad
Browsers get mad that the returned location address is not the same as what they sent, since the :(80|443) is stripped. Add a simple eunit test due to existing ct websockets tests not covering this case.
This commit is contained in:
parent
f03dbf4d06
commit
bebe3dc5d2
2 changed files with 28 additions and 7 deletions
|
@ -17,6 +17,7 @@
|
||||||
-export([handler_loop/4]). %% Internal.
|
-export([handler_loop/4]). %% Internal.
|
||||||
|
|
||||||
-include("include/http.hrl").
|
-include("include/http.hrl").
|
||||||
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
handler :: module(),
|
handler :: module(),
|
||||||
|
@ -102,12 +103,18 @@ websocket_handshake(State=#state{origin=Origin, challenge=Challenge},
|
||||||
|
|
||||||
-spec websocket_location(atom(), binary(), inet:ip_port(), binary())
|
-spec websocket_location(atom(), binary(), inet:ip_port(), binary())
|
||||||
-> binary().
|
-> binary().
|
||||||
websocket_location(ssl, Host, Port, Path) ->
|
websocket_location(Protocol, Host, Port, Path) ->
|
||||||
<< "wss://", Host/binary, ":",
|
<< (websocket_location_protocol(Protocol))/binary, "://", Host/binary,
|
||||||
(list_to_binary(integer_to_list(Port)))/binary, Path/binary >>;
|
(websocket_location_port(ssl, Port))/binary, Path/binary >>.
|
||||||
websocket_location(_Any, Host, Port, Path) ->
|
|
||||||
<< "ws://", Host/binary, ":",
|
-spec websocket_location_protocol(atom()) -> binary().
|
||||||
(list_to_binary(integer_to_list(Port)))/binary, Path/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.
|
-spec handler_before_loop(#state{}, #http_req{}, any(), binary()) -> ok.
|
||||||
handler_before_loop(State=#state{hibernate=true},
|
handler_before_loop(State=#state{hibernate=true},
|
||||||
|
@ -218,3 +225,17 @@ handler_terminate(#state{handler=Handler, opts=Opts},
|
||||||
[Handler, Class, Reason, TerminateReason, Opts,
|
[Handler, Class, Reason, TerminateReason, Opts,
|
||||||
HandlerState, Req, erlang:get_stacktrace()])
|
HandlerState, Req, erlang:get_stacktrace()])
|
||||||
end.
|
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.
|
||||||
|
|
||||||
|
|
|
@ -212,7 +212,7 @@ websocket(Config) ->
|
||||||
[Headers, Body] = websocket_headers(erlang:decode_packet(httph, Rest, []), []),
|
[Headers, Body] = websocket_headers(erlang:decode_packet(httph, Rest, []), []),
|
||||||
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
||||||
{'Upgrade', "WebSocket"} = lists:keyfind('Upgrade', 1, Headers),
|
{'Upgrade', "WebSocket"} = lists:keyfind('Upgrade', 1, Headers),
|
||||||
{"sec-websocket-location", "ws://localhost:80/websocket"}
|
{"sec-websocket-location", "ws://localhost/websocket"}
|
||||||
= lists:keyfind("sec-websocket-location", 1, Headers),
|
= lists:keyfind("sec-websocket-location", 1, Headers),
|
||||||
{"sec-websocket-origin", "http://localhost"}
|
{"sec-websocket-origin", "http://localhost"}
|
||||||
= lists:keyfind("sec-websocket-origin", 1, Headers),
|
= lists:keyfind("sec-websocket-origin", 1, Headers),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue