mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-15 04:30:25 +00:00
Merge branch 'hybi-framing-fix' of https://github.com/smarkets/cowboy
This commit is contained in:
commit
fe41f2944b
2 changed files with 75 additions and 7 deletions
|
@ -231,20 +231,26 @@ websocket_data(State=#state{version=0, eop=EOP}, Req, HandlerState,
|
||||||
%% @todo We probably should allow limiting frame length.
|
%% @todo We probably should allow limiting frame length.
|
||||||
handler_before_loop(State, Req, HandlerState, Data)
|
handler_before_loop(State, Req, HandlerState, Data)
|
||||||
end;
|
end;
|
||||||
|
%% incomplete hybi data frame.
|
||||||
|
websocket_data(State=#state{version=Version}, Req, HandlerState, Data)
|
||||||
|
when Version =/= 0, byte_size(Data) =:= 1 ->
|
||||||
|
handler_before_loop(State, Req, HandlerState, Data);
|
||||||
%% hybi data frame.
|
%% hybi data frame.
|
||||||
%% @todo Handle Fin.
|
%% @todo Handle Fin.
|
||||||
websocket_data(State=#state{version=Version}, Req, HandlerState, Data)
|
websocket_data(State=#state{version=Version}, Req, HandlerState, Data)
|
||||||
when Version =/= 0 ->
|
when Version =/= 0 ->
|
||||||
<< 1:1, 0:3, Opcode:4, Mask:1, PayloadLen:7, Rest/bits >> = Data,
|
<< 1:1, 0:3, Opcode:4, Mask:1, PayloadLen:7, Rest/bits >> = Data,
|
||||||
{PayloadLen2, Rest2} = case PayloadLen of
|
{PayloadLen2, Rest2} = case {PayloadLen, Rest} of
|
||||||
126 -> << L:16, R/bits >> = Rest, {L, R};
|
{126, << L:16, R/bits >>} -> {L, R};
|
||||||
127 -> << 0:1, L:63, R/bits >> = Rest, {L, R};
|
{126, Rest} -> {undefined, Rest};
|
||||||
PayloadLen -> {PayloadLen, Rest}
|
{127, << 0:1, L:63, R/bits >>} -> {L, R};
|
||||||
|
{127, Rest} -> {undefined, Rest};
|
||||||
|
{PayloadLen, Rest} -> {PayloadLen, Rest}
|
||||||
end,
|
end,
|
||||||
case {Mask, PayloadLen2} of
|
case {Mask, PayloadLen2} of
|
||||||
{0, 0} ->
|
{0, 0} ->
|
||||||
websocket_dispatch(State, Req, HandlerState, Rest2, Opcode, <<>>);
|
websocket_dispatch(State, Req, HandlerState, Rest2, Opcode, <<>>);
|
||||||
{1, N} when N + 4 < byte_size(Rest2) ->
|
{1, N} when N + 4 > byte_size(Rest2); N =:= undefined ->
|
||||||
%% @todo We probably should allow limiting frame length.
|
%% @todo We probably should allow limiting frame length.
|
||||||
handler_before_loop(State, Req, HandlerState, Data);
|
handler_before_loop(State, Req, HandlerState, Data);
|
||||||
{1, _N} ->
|
{1, _N} ->
|
||||||
|
|
|
@ -20,7 +20,8 @@
|
||||||
init_per_group/2, end_per_group/2]). %% ct.
|
init_per_group/2, end_per_group/2]). %% ct.
|
||||||
-export([chunked_response/1, headers_dupe/1, headers_huge/1,
|
-export([chunked_response/1, headers_dupe/1, headers_huge/1,
|
||||||
keepalive_nl/1, nc_rand/1, pipeline/1, raw/1,
|
keepalive_nl/1, nc_rand/1, pipeline/1, raw/1,
|
||||||
ws0/1, ws8/1, ws_timeout_hibernate/1]). %% http.
|
ws0/1, ws8/1, ws8_single_bytes/1,
|
||||||
|
ws_timeout_hibernate/1]). %% http.
|
||||||
-export([http_200/1, http_404/1]). %% http and https.
|
-export([http_200/1, http_404/1]). %% http and https.
|
||||||
-export([http_10_hostless/1]). %% misc.
|
-export([http_10_hostless/1]). %% misc.
|
||||||
|
|
||||||
|
@ -33,7 +34,8 @@ groups() ->
|
||||||
BaseTests = [http_200, http_404],
|
BaseTests = [http_200, http_404],
|
||||||
[{http, [], [chunked_response, headers_dupe, headers_huge,
|
[{http, [], [chunked_response, headers_dupe, headers_huge,
|
||||||
keepalive_nl, nc_rand, pipeline, raw,
|
keepalive_nl, nc_rand, pipeline, raw,
|
||||||
ws0, ws8, ws_timeout_hibernate] ++ BaseTests},
|
ws0, ws8, ws8_single_bytes,
|
||||||
|
ws_timeout_hibernate] ++ BaseTests},
|
||||||
{https, [], BaseTests}, {misc, [], [http_10_hostless]}].
|
{https, [], BaseTests}, {misc, [], [http_10_hostless]}].
|
||||||
|
|
||||||
init_per_suite(Config) ->
|
init_per_suite(Config) ->
|
||||||
|
@ -304,6 +306,66 @@ ws8(Config) ->
|
||||||
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
ws8_single_bytes(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 /websocket 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: 8\r\n"
|
||||||
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\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, <<>>] = websocket_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),
|
||||||
|
ok = gen_tcp:send(Socket, << 16#81 >>), %% send one byte
|
||||||
|
ok = timer:sleep(100), %% sleep for a period
|
||||||
|
ok = gen_tcp:send(Socket, << 16#85 >>), %% send another and so on
|
||||||
|
ok = timer:sleep(100),
|
||||||
|
ok = gen_tcp:send(Socket, << 16#37 >>),
|
||||||
|
ok = timer:sleep(100),
|
||||||
|
ok = gen_tcp:send(Socket, << 16#fa >>),
|
||||||
|
ok = timer:sleep(100),
|
||||||
|
ok = gen_tcp:send(Socket, << 16#21 >>),
|
||||||
|
ok = timer:sleep(100),
|
||||||
|
ok = gen_tcp:send(Socket, << 16#3d >>),
|
||||||
|
ok = timer:sleep(100),
|
||||||
|
ok = gen_tcp:send(Socket, << 16#7f >>),
|
||||||
|
ok = timer:sleep(100),
|
||||||
|
ok = gen_tcp:send(Socket, << 16#9f >>),
|
||||||
|
ok = timer:sleep(100),
|
||||||
|
ok = gen_tcp:send(Socket, << 16#4d >>),
|
||||||
|
ok = timer:sleep(100),
|
||||||
|
ok = gen_tcp:send(Socket, << 16#51 >>),
|
||||||
|
ok = timer:sleep(100),
|
||||||
|
ok = gen_tcp:send(Socket, << 16#58 >>),
|
||||||
|
{ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
|
||||||
|
= gen_tcp:recv(Socket, 0, 6000),
|
||||||
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
|
||||||
|
= gen_tcp:recv(Socket, 0, 6000),
|
||||||
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
||||||
|
= gen_tcp:recv(Socket, 0, 6000),
|
||||||
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
||||||
|
= gen_tcp:recv(Socket, 0, 6000),
|
||||||
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
||||||
|
= gen_tcp:recv(Socket, 0, 6000),
|
||||||
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
|
||||||
|
{ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
|
||||||
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
|
||||||
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
||||||
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
||||||
|
ok.
|
||||||
|
|
||||||
ws_timeout_hibernate(Config) ->
|
ws_timeout_hibernate(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,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue