Skip to content

Commit 2b8d980

Browse files
committed
Merge pull request #36 from ypaq/th-custom-headers
Add options to enable extra headers
2 parents f0334f0 + 652f24c commit 2b8d980

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/websocket_client.erl

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@
1111

1212
-export([ws_client_init/7]).
1313

14+
-type opt() :: {async_start, boolean()}
15+
| {extra_headers, [{string() | binary(), string() | binary()}]}
16+
.
17+
18+
-type opts() :: [opt()].
19+
1420
%% @doc Start the websocket client
15-
-spec start_link(URL :: string(), Handler :: module(), Args :: list()) ->
21+
-spec start_link(URL :: string(), Handler :: module(), HandlerArgs :: list()) ->
1622
{ok, pid()} | {error, term()}.
17-
start_link(URL, Handler, Args) ->
18-
start_link(URL, Handler, Args, true).
23+
start_link(URL, Handler, HandlerArgs) ->
24+
start_link(URL, Handler, HandlerArgs, []).
1925

20-
start_link(URL, Handler, Args, AsyncStart) ->
26+
start_link(URL, Handler, HandlerArgs, AsyncStart) when is_boolean(AsyncStart) ->
27+
start_link(URL, Handler, HandlerArgs, [{async_start, AsyncStart}]);
28+
start_link(URL, Handler, HandlerArgs, Opts) when is_list(Opts) ->
2129
case http_uri:parse(URL, [{scheme_defaults, [{ws,80},{wss,443}]}]) of
2230
{ok, {Protocol, _, Host, Port, Path, Query}} ->
2331
proc_lib:start_link(?MODULE, ws_client_init,
24-
[Handler, Protocol, Host, Port, Path ++ Query, Args, AsyncStart]);
32+
[Handler, Protocol, Host, Port, Path ++ Query, HandlerArgs, Opts]);
2533
{error, _} = Error ->
2634
Error
2735
end.
@@ -36,9 +44,9 @@ cast(Client, Frame) ->
3644
%% @doc Create socket, execute handshake, and enter loop
3745
-spec ws_client_init(Handler :: module(), Protocol :: websocket_req:protocol(),
3846
Host :: string(), Port :: inet:port_number(), Path :: string(),
39-
Args :: list(), AsyncStart :: boolean()) ->
47+
Args :: list(), Opts :: opts()) ->
4048
no_return().
41-
ws_client_init(Handler, Protocol, Host, Port, Path, Args, AsyncStart) ->
49+
ws_client_init(Handler, Protocol, Host, Port, Path, Args, Opts) ->
4250
Transport = case Protocol of
4351
wss ->
4452
ssl;
@@ -76,11 +84,13 @@ ws_client_init(Handler, Protocol, Host, Port, Path, Args, AsyncStart) ->
7684
Handler,
7785
generate_ws_key()
7886
),
79-
case websocket_handshake(WSReq) of
87+
ExtraHeaders = proplists:get_value(extra_headers, Opts, []),
88+
case websocket_handshake(WSReq, ExtraHeaders) of
8089
{error, _} = HandshakeError ->
8190
proc_lib:init_ack(HandshakeError),
8291
exit(normal);
8392
{ok, Buffer} ->
93+
AsyncStart = proplists:get_value(async_start, Opts, true),
8494
AsyncStart andalso proc_lib:init_ack({ok, self()}),
8595
{ok, HandlerState, KeepAlive} = case Handler:init(Args, WSReq) of
8696
{ok, HS} ->
@@ -110,8 +120,8 @@ ws_client_init(Handler, Protocol, Host, Port, Path, Args, AsyncStart) ->
110120
end.
111121

112122
%% @doc Send http upgrade request and validate handshake response challenge
113-
-spec websocket_handshake(WSReq :: websocket_req:req()) -> {ok, binary()} | {error, term()}.
114-
websocket_handshake(WSReq) ->
123+
-spec websocket_handshake(WSReq :: websocket_req:req(), [{string(), string()}]) -> {ok, binary()} | {error, term()}.
124+
websocket_handshake(WSReq, ExtraHeaders) ->
115125
[Protocol, Path, Host, Key, Transport, Socket] =
116126
websocket_req:get([protocol, path, host, key, transport, socket], WSReq),
117127
Handshake = ["GET ", Path, " HTTP/1.1\r\n"
@@ -120,7 +130,8 @@ websocket_handshake(WSReq) ->
120130
"Origin: ", atom_to_binary(Protocol, utf8), "://", Host, "\r\n"
121131
"Sec-WebSocket-Version: 13\r\n"
122132
"Sec-WebSocket-Key: ", Key, "\r\n"
123-
"Upgrade: websocket\r\n"
133+
"Upgrade: websocket\r\n",
134+
[ [Header, ": ", Value, "\r\n"] || {Header, Value} <- ExtraHeaders],
124135
"\r\n"],
125136
Transport:send(Socket, Handshake),
126137
{ok, HandshakeResponse} = receive_handshake(<<>>, Transport, Socket),

0 commit comments

Comments
 (0)