0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +00:00

Add support for fragmented websocket messages

This commit is contained in:
Magnus Klaar 2012-04-01 18:58:28 -07:00
parent 4fb2a6face
commit 0b15ed914d
2 changed files with 172 additions and 29 deletions

View file

@ -19,7 +19,7 @@
-export([all/0, groups/0, init_per_suite/1, end_per_suite/1,
init_per_group/2, end_per_group/2]). %% ct.
-export([ws0/1, ws8/1, ws8_single_bytes/1, ws8_init_shutdown/1,
ws13/1, ws_timeout_hibernate/1]). %% ws.
ws13/1, ws_timeout_hibernate/1, ws_text_fragments/1]). %% ws.
%% ct.
@ -28,7 +28,7 @@ all() ->
groups() ->
BaseTests = [ws0, ws8, ws8_single_bytes, ws8_init_shutdown, ws13,
ws_timeout_hibernate],
ws_timeout_hibernate, ws_text_fragments],
[{ws, [], BaseTests}].
init_per_suite(Config) ->
@ -60,7 +60,8 @@ init_dispatch() ->
{[<<"localhost">>], [
{[<<"websocket">>], websocket_handler, []},
{[<<"ws_timeout_hibernate">>], ws_timeout_hibernate_handler, []},
{[<<"ws_init_shutdown">>], websocket_handler_init_shutdown, []}
{[<<"ws_init_shutdown">>], websocket_handler_init_shutdown, []},
{[<<"ws_echo_handler">>], websocket_echo_handler, []}
]}
].
@ -310,6 +311,62 @@ ws13(Config) ->
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
ok.
ws_text_fragments(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_echo_handler 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, [
<< 0:1, 0:3, 1:4, 1:1, 5:7 >>,
<< 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
<< 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
ok = gen_tcp:send(Socket, [
<< 1:1, 0:3, 0:4, 1:1, 5:7 >>,
<< 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
<< 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
{ok, << 1:1, 0:3, 1:4, 0:1, 10:7, "HelloHello" >>}
= gen_tcp:recv(Socket, 0, 6000),
ok = gen_tcp:send(Socket, [
%% #1
<< 0:1, 0:3, 1:4, 1:1, 5:7 >>,
<< 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
<< 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>,
%% #2
<< 0:1, 0:3, 0:4, 1:1, 5:7 >>,
<< 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
<< 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>,
%% #3
<< 1:1, 0:3, 0:4, 1:1, 5:7 >>,
<< 16#37 >>, << 16#fa >>, << 16#21 >>, << 16#3d >>, << 16#7f >>,
<< 16#9f >>, << 16#4d >>, << 16#51 >>, << 16#58 >>]),
{ok, << 1:1, 0:3, 1:4, 0:1, 15:7, "HelloHelloHello" >>}
= gen_tcp:recv(Socket, 0, 6000),
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.
websocket_headers({ok, http_eoh, Rest}, Acc) ->
[Acc, Rest];
websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->