aboutsummaryrefslogtreecommitdiffstats
path: root/src/gun.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2015-04-10 16:34:21 +0300
committerLoïc Hoguin <[email protected]>2015-04-10 16:34:21 +0300
commitcff0a87d3cbdcf67a9049cdc2784d459711e2867 (patch)
tree6eabc302b146843e8d00bd761be41b32d8f478d3 /src/gun.erl
parentc46991067a53c81316a69c1df7c7dc590f3ca308 (diff)
downloadgun-cff0a87d3cbdcf67a9049cdc2784d459711e2867.tar.gz
gun-cff0a87d3cbdcf67a9049cdc2784d459711e2867.tar.bz2
gun-cff0a87d3cbdcf67a9049cdc2784d459711e2867.zip
Add Websocket options
Allow passing Websocket options through either open or ws_upgrade. Document ws_upgrade/4.
Diffstat (limited to 'src/gun.erl')
-rw-r--r--src/gun.erl32
1 files changed, 25 insertions, 7 deletions
diff --git a/src/gun.erl b/src/gun.erl
index 5d564a5..ae30494 100644
--- a/src/gun.erl
+++ b/src/gun.erl
@@ -169,6 +169,13 @@ check_options([{transport, T}|Opts]) when T =:= tcp; T =:= ssl ->
check_options(Opts);
check_options([{transport_opts, L}|Opts]) when is_list(L) ->
check_options(Opts);
+check_options([{ws_opts, ProtoOpts}|Opts]) when is_map(ProtoOpts) ->
+ case gun_ws:check_options(ProtoOpts) of
+ ok ->
+ check_options(Opts);
+ Error ->
+ Error
+ end;
check_options([Opt|_]) ->
{error, {options, Opt}}.
@@ -421,14 +428,17 @@ cancel(ServerPid, StreamRef) ->
-spec ws_upgrade(pid(), iodata()) -> reference().
ws_upgrade(ServerPid, Path) ->
- ws_upgrade(ServerPid, Path, [], #{}).
+ ws_upgrade(ServerPid, Path, []).
-spec ws_upgrade(pid(), iodata(), headers()) -> reference().
ws_upgrade(ServerPid, Path, Headers) ->
- ws_upgrade(ServerPid, Path, Headers, #{}).
+ StreamRef = make_ref(),
+ _ = ServerPid ! {ws_upgrade, self(), StreamRef, Path, Headers},
+ StreamRef.
-spec ws_upgrade(pid(), iodata(), headers(), ws_opts()) -> reference().
ws_upgrade(ServerPid, Path, Headers, Opts) ->
+ ok = gun_ws:check_options(Opts),
StreamRef = make_ref(),
_ = ServerPid ! {ws_upgrade, self(), StreamRef, Path, Headers, Opts},
StreamRef.
@@ -545,7 +555,7 @@ before_loop(State=#state{opts=Opts, protocol=Protocol}) ->
KeepaliveRef = erlang:send_after(Keepalive, self(), keepalive),
loop(State#state{keepalive_ref=KeepaliveRef}).
-loop(State=#state{parent=Parent, owner=Owner, host=Host, port=Port,
+loop(State=#state{parent=Parent, owner=Owner, host=Host, port=Port, opts=Opts,
socket=Socket, transport=Transport, protocol=Protocol, protocol_state=ProtoState}) ->
{OK, Closed, Error} = Transport:messages(),
Transport:setopts(Socket, [{active, once}]),
@@ -592,6 +602,10 @@ loop(State=#state{parent=Parent, owner=Owner, host=Host, port=Port,
{cancel, Owner, StreamRef} ->
ProtoState2 = Protocol:cancel(ProtoState, StreamRef),
loop(State#state{protocol_state=ProtoState2});
+ {ws_upgrade, Owner, StreamRef, Path, Headers} when Protocol =/= gun_spdy ->
+ WsOpts = maps:get(ws_opts, Opts, #{}),
+ ProtoState2 = Protocol:ws_upgrade(ProtoState, StreamRef, Host, Port, Path, Headers, WsOpts),
+ loop(State#state{protocol_state=ProtoState2});
{ws_upgrade, Owner, StreamRef, Path, Headers, WsOpts} when Protocol =/= gun_spdy ->
ProtoState2 = Protocol:ws_upgrade(ProtoState, StreamRef, Host, Port, Path, Headers, WsOpts),
loop(State#state{protocol_state=ProtoState2});
@@ -605,19 +619,23 @@ loop(State=#state{parent=Parent, owner=Owner, host=Host, port=Port,
{dbg_send_raw, Owner, Data} ->
Transport:send(Socket, Data),
loop(State);
- Any when is_tuple(Any), is_pid(element(2, Any)) ->
- element(2, Any) ! {gun_error, self(), {notowner,
- "Operations are restricted to the owner of the connection."}},
- loop(State);
{ws_upgrade, _, StreamRef, _, _} ->
Owner ! {gun_error, self(), StreamRef, {badstate,
"Websocket is only supported over HTTP/1.1."}},
loop(State);
+ {ws_upgrade, _, StreamRef, _, _, _} ->
+ Owner ! {gun_error, self(), StreamRef, {badstate,
+ "Websocket is only supported over HTTP/1.1."}},
+ loop(State);
{ws_send, _, _} ->
Owner ! {gun_error, self(), {badstate,
"Connection needs to be upgraded to Websocket "
"before the gun:ws_send/1 function can be used."}},
loop(State);
+ Any when is_tuple(Any), is_pid(element(2, Any)) ->
+ element(2, Any) ! {gun_error, self(), {notowner,
+ "Operations are restricted to the owner of the connection."}},
+ loop(State);
Any ->
error_logger:error_msg("Unexpected message: ~w~n", [Any]),
loop(State)