Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom headers with websocket request #296

Merged
merged 2 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/conf-sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,16 @@ If not set this defaults to the ``host`` value.

<websocket type="connect" origin="https://example.com"></websocket>

**New in 1.7.1**: You can also add any HTTP header now, as in:

.. code-block:: xml

<request subst="true">
<websocket type="connect" path="/path/to/ws">
<http_header name="Cookie" value="sessionid=%%_sessionid%%"/>
</websocket>
</request>

AMQP
^^^^^^^^^
.. _sec-session-amqp-label:
Expand Down
1 change: 1 addition & 0 deletions include/ts_websocket.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
frame = "binary",
origin, % origin of websocket request
subprotos = [], % subprotocols
headers = [], % additional headers
data % websocket data
}).

Expand Down
22 changes: 19 additions & 3 deletions src/lib/websocket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
-vc('$Id$ ').
-author('jzhihui521@gmail.com').

-export([get_handshake/5, check_handshake/2, encode_binary/1, encode_text/1,
-export([get_handshake/6, check_handshake/2, encode_binary/1, encode_text/1,
encode_close/1, encode/2, decode/1]).

-include("ts_profile.hrl").
Expand All @@ -37,7 +37,7 @@
%%%===================================================================
%%% API functions
%%%===================================================================
get_handshake(Host, Path, SubProtocol, Version, Origin) ->
get_handshake(Host, Path, SubProtocol, Version, Origin, Headers) ->
{Key, Accept} = gen_accept_key(),
Req = list_to_binary(["GET ", Path, " HTTP/1.1\r\n",
"Host: ", Host ,"\r\n",
Expand All @@ -48,7 +48,8 @@ get_handshake(Host, Path, SubProtocol, Version, Origin) ->
_ -> Origin
end, "\r\n",
"Sec-WebSocket-Key: ", Key, "\r\n",
"Sec-WebSocket-Version: ", Version, "\r\n"]),
"Sec-WebSocket-Version: ", Version, "\r\n",
headers(Headers)]),
SubProHeader = case SubProtocol of
[] -> [];
_ -> "Sec-WebSocket-Protocol: " ++ SubProtocol ++ "\r\n"
Expand Down Expand Up @@ -196,3 +197,18 @@ parse_frame(<< 1:1, % FIN
parse_payload(Opcode, MaskLengthAndPayload);
parse_frame(_Data) ->
more.

% user defined headers
headers([]) -> [];
headers(Headers) ->
HeadersToIgnore = ["host", "upgrade", "connection", "origin",
"sec-websocket-key", "sec-websocket-version",
"sec-websocket-protocol"],
lists:foldl(fun({Name, Value}, Result) ->
case lists:member(string:to_lower(Name), HeadersToIgnore) of
true ->
Result;
_ ->
[Name, ": ", Value, ?CRLF | Result]
end
end, [], lists:reverse(Headers)).
2 changes: 1 addition & 1 deletion src/test/ts_test_websocket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
test()->ok.

handshake_test() ->
{_, Accept} = websocket:get_handshake("127.0.0.1", "/chat", [], 13, ""),
{_, Accept} = websocket:get_handshake("127.0.0.1", "/chat", [], 13, "", []),
Response1 = ["HTTP/1.1 101 Switching Protocols\r\n",
"Upgrade: websocket\r\n",
"Connection: Upgrade\r\n",
Expand Down
13 changes: 9 additions & 4 deletions src/tsung/ts_websocket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ dump(A,B) ->
%%----------------------------------------------------------------------
get_message(#websocket_request{type = connect, path = Path,
subprotos = SubProtocol, version = Version,
origin = Origin},
headers = Headers, origin = Origin},
State=#state_rcv{session = WebsocketSession}) ->
{Request, Accept} = websocket:get_handshake(State#state_rcv.host, Path,
SubProtocol, Version, Origin),
SubProtocol, Version, Origin,
Headers),
{Request, WebsocketSession#websocket_session{status = waiting_handshake,
accept = Accept}};
get_message(#websocket_request{type = message, data = Data, frame = Frame},
Expand Down Expand Up @@ -175,9 +176,13 @@ add_dynparams(true, {DynVars, _S},
NewData = ts_search:subst(Data, DynVars),
Param#websocket_request{data = NewData};
add_dynparams(true, {DynVars, _S},
Param = #websocket_request{type = connect, path = Path},
Param = #websocket_request{type = connect, path = Path,
headers = Headers},
_HostData) ->
NewPath = ts_search:subst(Path, DynVars),
Param#websocket_request{path = NewPath};
NewHeaders = lists:foldl(fun ({Name, Value}, Result) ->
[{Name, ts_search:subst(Value, DynVars)} | Result]
end, [], Headers),
Param#websocket_request{path = NewPath, headers = NewHeaders};
add_dynparams(_Bool, _DynData, Param, _HostData) ->
Param#websocket_request{}.
9 changes: 8 additions & 1 deletion src/tsung_controller/ts_config_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
-author('nicolas.niclausse@niclux.org').

-export([parse_config/2, parse_URL/1, set_port/1, set_scheme/1,
check_user_agent_sum/1, set_query/1, encode_ipv6_address/1]).
check_user_agent_sum/1, set_query/1, encode_ipv6_address/1,
parse_headers/2]).

-include("ts_profile.hrl").
-include("ts_http.hrl").
Expand Down Expand Up @@ -209,6 +210,12 @@ get_previous_http_server(Ets, Id) ->
[{_Key,PrevServ}] -> PrevServ
end.

%%----------------------------------------------------------------------
%% Func: parse_headers/2
%% Args: Elements (list), Headers (list)
%% Returns: List
%% Purpose: parse http_header elements
%%----------------------------------------------------------------------
parse_headers([], Headers) ->
Headers;
parse_headers([Element = #xmlElement{name=http_header} | Tail], Headers) ->
Expand Down
3 changes: 2 additions & 1 deletion src/tsung_controller/ts_config_websocket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ parse_config(Element = #xmlElement{name = websocket},
SubProtocols = ts_config:getAttr(string, Element#xmlElement.attributes, subprotocols, ""),
Frame = ts_config:getAttr(string, Element#xmlElement.attributes, frame,
"binary"),
Headers = ts_config_http:parse_headers(Element#xmlElement.content, []),

Request = #websocket_request{data = ValRaw, type = Type, subprotos = SubProtocols,
origin = Origin, path = Path, frame = Frame},
origin = Origin, path = Path, frame = Frame, headers = Headers},

Ack = case Type of
message ->
Expand Down
2 changes: 1 addition & 1 deletion tsung-1.0.dtd
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ repeat | if | change_type | foreach | set_option | interaction | abort )*>
certfile CDATA #IMPLIED
dn CDATA #IMPLIED
>
<!ELEMENT websocket (#PCDATA) >
<!ELEMENT websocket (#PCDATA | http_header)* >
<!ATTLIST websocket
type (connect | message | close) #REQUIRED
ack (no_ack | parse) #IMPLIED
Expand Down