aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrinpatch <[email protected]>2020-09-24 00:06:51 +0300
committerLoïc Hoguin <[email protected]>2020-10-07 11:12:59 +0200
commitc2c0203a5c1e8bcf14b7a26d53c3cb7cc6fd565b (patch)
tree758d20bf40a2046a5146b7f6954cdee60bfd2f46
parent2f4046c13213ed2cd7d51d6422e74170bf22d2fc (diff)
downloadgun-c2c0203a5c1e8bcf14b7a26d53c3cb7cc6fd565b.tar.gz
gun-c2c0203a5c1e8bcf14b7a26d53c3cb7cc6fd565b.tar.bz2
gun-c2c0203a5c1e8bcf14b7a26d53c3cb7cc6fd565b.zip
Normalize headers in ws_upgrade
In the documentation headers passed to ws_upgrade are typed as gun:req_headers(), however if a map of headers is passed (which is allowed by the type), the gun process will crash when trying to operate on it as if it were a list.
-rw-r--r--src/gun.erl4
-rw-r--r--test/ws_SUITE.erl11
2 files changed, 13 insertions, 2 deletions
diff --git a/src/gun.erl b/src/gun.erl
index 047aa18..bd4ce3d 100644
--- a/src/gun.erl
+++ b/src/gun.erl
@@ -920,7 +920,7 @@ ws_upgrade(ServerPid, Path) ->
-spec ws_upgrade(pid(), iodata(), req_headers()) -> stream_ref().
ws_upgrade(ServerPid, Path, Headers) ->
StreamRef = make_ref(),
- gen_statem:cast(ServerPid, {ws_upgrade, self(), StreamRef, Path, Headers}),
+ gen_statem:cast(ServerPid, {ws_upgrade, self(), StreamRef, Path, normalize_headers(Headers)}),
StreamRef.
-spec ws_upgrade(pid(), iodata(), req_headers(), ws_opts()) -> stream_ref().
@@ -929,7 +929,7 @@ ws_upgrade(ServerPid, Path, Headers, Opts) ->
StreamRef = make_ref(),
ReplyTo = maps:get(reply_to, Opts, self()),
%% @todo Also accept tunnel option.
- gen_statem:cast(ServerPid, {ws_upgrade, ReplyTo, StreamRef, Path, Headers, Opts}),
+ gen_statem:cast(ServerPid, {ws_upgrade, ReplyTo, StreamRef, Path, normalize_headers(Headers), Opts}),
StreamRef.
%% @todo ws_send/2 will need to be deprecated in favor of a variant with StreamRef.
diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl
index d4413fb..c04acc2 100644
--- a/test/ws_SUITE.erl
+++ b/test/ws_SUITE.erl
@@ -70,6 +70,17 @@ error_http10_upgrade(Config) ->
error(timeout)
end.
+headers_normalized_upgrade(Config) ->
+ doc("Headers passed to ws_upgrade are normalized before being used."),
+ {ok, ConnPid} = gun:open("localhost", config(port, Config)),
+ {ok, _} = gun:await_up(ConnPid),
+ StreamRef = gun:ws_upgrade(ConnPid, "/", #{
+ atom_header_name => <<"value">>,
+ "string_header_name" => <<"value">>
+ }),
+ {upgrade, [<<"websocket">>], _} = gun:await(ConnPid, StreamRef),
+ gun:close(ConnPid).
+
error_http_request(Config) ->
doc("Ensure that requests are rejected while using Websocket."),
{ok, ConnPid} = gun:open("localhost", config(port, Config)),