0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00
This commit is contained in:
Loïc Hoguin 2016-08-12 17:01:01 +02:00
commit e44ac98c83
4 changed files with 55 additions and 5 deletions

View file

@ -77,6 +77,9 @@
-export([push/3]). -export([push/3]).
-export([push/4]). -export([push/4]).
%% Internal.
-export([response_headers/2]).
-type cookie_opts() :: cow_cookie:cookie_opts(). -type cookie_opts() :: cow_cookie:cookie_opts().
-export_type([cookie_opts/0]). -export_type([cookie_opts/0]).

View file

@ -158,12 +158,11 @@ websocket_handshake(State=#state{key=Key},
Req=#{pid := Pid, streamid := StreamID}, HandlerState, Env) -> Req=#{pid := Pid, streamid := StreamID}, HandlerState, Env) ->
Challenge = base64:encode(crypto:hash(sha, Challenge = base64:encode(crypto:hash(sha,
<< Key/binary, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" >>)), << Key/binary, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" >>)),
Headers = #{ Headers = cowboy_req:response_headers(#{
%% @todo Hmm should those be here or in cowboy_http?
<<"connection">> => <<"Upgrade">>, <<"connection">> => <<"Upgrade">>,
<<"upgrade">> => <<"websocket">>, <<"upgrade">> => <<"websocket">>,
<<"sec-websocket-accept">> => Challenge <<"sec-websocket-accept">> => Challenge
}, }, Req),
Pid ! {{Pid, StreamID}, {switch_protocol, Headers, ?MODULE, {Req, State, HandlerState}}}, Pid ! {{Pid, StreamID}, {switch_protocol, Headers, ?MODULE, {Req, State, HandlerState}}},
{ok, Req, Env}. {ok, Req, Env}.

View file

@ -78,6 +78,7 @@ init_dispatch() ->
{close, 1001, <<"some text!">>}, {close, 1001, <<"some text!">>},
{text, <<"won't be received">>}]} {text, <<"won't be received">>}]}
]}, ]},
{"/ws_subprotocol", ws_subprotocol, []},
{"/ws_timeout_hibernate", ws_timeout_hibernate, []}, {"/ws_timeout_hibernate", ws_timeout_hibernate, []},
{"/ws_timeout_cancel", ws_timeout_cancel, []} {"/ws_timeout_cancel", ws_timeout_cancel, []}
]} ]}
@ -522,6 +523,35 @@ ws_send_many(Config) ->
{error, closed} = gen_tcp:recv(Socket, 0, 6000), {error, closed} = gen_tcp:recv(Socket, 0, 6000),
ok. ok.
ws_subprotocol(Config) ->
{port, Port} = lists:keyfind(port, 1, Config),
{ok, Socket} = gen_tcp:connect("localhost", Port,
[binary, {active, false}, {packet, raw}]),
ok = gen_tcp:send(Socket, [
"GET /ws_subprotocol HTTP/1.1\r\n"
"Host: localhost\r\n"
"Connection: Upgrade\r\n"
"Upgrade: websocket\r\n"
"Sec-WebSocket-Origin: http://localhost\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Protocol: foo, bar\r\n"
"\r\n"]),
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
= erlang:decode_packet(http, Handshake, []),
[Headers, <<>>] = do_decode_headers(
erlang:decode_packet(httph, Rest, []), []),
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
= lists:keyfind("sec-websocket-accept", 1, Headers),
{"sec-websocket-protocol", "foo"}
= lists:keyfind("sec-websocket-protocol", 1, Headers),
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
ok.
ws_text_fragments(Config) -> ws_text_fragments(Config) ->
{port, Port} = lists:keyfind(port, 1, Config), {port, Port} = lists:keyfind(port, 1, Config),
{ok, Socket} = gen_tcp:connect("localhost", Port, {ok, Socket} = gen_tcp:connect("localhost", Port,
@ -603,7 +633,7 @@ ws_timeout_hibernate(Config) ->
ok. ok.
ws_timeout_cancel(Config) -> ws_timeout_cancel(Config) ->
%% Erlang messages to a socket should not cancel the timeout %% Erlang messages to a socket should not cancel the timeout
{port, Port} = lists:keyfind(port, 1, Config), {port, Port} = lists:keyfind(port, 1, Config),
{ok, Socket} = gen_tcp:connect("localhost", Port, {ok, Socket} = gen_tcp:connect("localhost", Port,
[binary, {active, false}, {packet, raw}]), [binary, {active, false}, {packet, raw}]),
@ -630,7 +660,7 @@ ws_timeout_cancel(Config) ->
ok. ok.
ws_timeout_reset(Config) -> ws_timeout_reset(Config) ->
%% Erlang messages across a socket should reset the timeout %% Erlang messages across a socket should reset the timeout
{port, Port} = lists:keyfind(port, 1, Config), {port, Port} = lists:keyfind(port, 1, Config),
{ok, Socket} = gen_tcp:connect("localhost", Port, {ok, Socket} = gen_tcp:connect("localhost", Port,
[binary, {active, false}, {packet, raw}]), [binary, {active, false}, {packet, raw}]),

View file

@ -0,0 +1,18 @@
%% Feel free to use, reuse and abuse the code in this file.
-module(ws_subprotocol).
-export([init/2]).
-export([websocket_handle/3]).
-export([websocket_info/3]).
init(Req, Opts) ->
[Protocol | _] = cowboy_req:parse_header(<<"sec-websocket-protocol">>, Req),
Req2 = cowboy_req:set_resp_header(<<"sec-websocket-protocol">>, Protocol, Req),
{cowboy_websocket, Req2, Opts, 1000}.
websocket_handle(_Frame, Req, State) ->
{ok, Req, State}.
websocket_info(_Info, Req, State) ->
{ok, Req, State}.