| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
-module(ws_SUITE). |
| 16 |
-compile(export_all). |
| 17 |
-compile(nowarn_export_all). |
| 18 |
|
| 19 |
-import(ct_helper, [config/2]). |
| 20 |
-import(ct_helper, [doc/1]). |
| 21 |
|
| 22 |
%% ct. |
| 23 |
|
| 24 |
all() -> |
| 25 |
[{group, ws}]. |
| 26 |
|
| 27 |
groups() -> |
| 28 |
[{ws, [parallel], ct_helper:all(?MODULE)}]. |
| 29 |
|
| 30 |
init_per_group(Name, Config) -> |
| 31 |
cowboy_test:init_http(Name, #{ |
| 32 |
env => #{dispatch => init_dispatch()} |
| 33 |
}, Config). |
| 34 |
|
| 35 |
end_per_group(Listener, _Config) -> |
| 36 |
cowboy:stop_listener(Listener). |
| 37 |
|
| 38 |
%% Dispatch configuration. |
| 39 |
|
| 40 |
init_dispatch() -> |
| 41 |
cowboy_router:compile([ |
| 42 |
{"localhost", [ |
| 43 |
{"/ws_echo", ws_echo, []}, |
| 44 |
{"/ws_echo_timer", ws_echo_timer, []}, |
| 45 |
{"/ws_init", ws_init_h, #{}}, |
| 46 |
{"/ws_init_shutdown", ws_init_shutdown, []}, |
| 47 |
{"/ws_send_many", ws_send_many, [ |
| 48 |
{sequence, [ |
| 49 |
{text, <<"one">>}, |
| 50 |
{text, <<"two">>}, |
| 51 |
{text, <<"seven!">>}]} |
| 52 |
]}, |
| 53 |
{"/ws_send_close", ws_send_many, [ |
| 54 |
{sequence, [ |
| 55 |
{text, <<"send">>}, |
| 56 |
close, |
| 57 |
{text, <<"won't be received">>}]} |
| 58 |
]}, |
| 59 |
{"/ws_send_close_payload", ws_send_many, [ |
| 60 |
{sequence, [ |
| 61 |
{text, <<"send">>}, |
| 62 |
{close, 1001, <<"some text!">>}, |
| 63 |
{text, <<"won't be received">>}]} |
| 64 |
]}, |
| 65 |
{"/ws_subprotocol", ws_subprotocol, []}, |
| 66 |
{"/terminate", ws_terminate_h, []}, |
| 67 |
{"/ws_timeout_hibernate", ws_timeout_hibernate, []}, |
| 68 |
{"/ws_timeout_cancel", ws_timeout_cancel, []}, |
| 69 |
{"/ws_max_frame_size", ws_max_frame_size, []}, |
| 70 |
{"/ws_max_frame_size_deflate", ws_max_frame_size_deflate, []}, |
| 71 |
{"/ws_deflate_opts", ws_deflate_opts_h, []}, |
| 72 |
{"/ws_dont_validate_utf8", ws_dont_validate_utf8_h, []}, |
| 73 |
{"/ws_ping", ws_ping_h, []} |
| 74 |
]} |
| 75 |
]). |
| 76 |
|
| 77 |
%% Tests. |
| 78 |
|
| 79 |
unlimited_connections(Config) -> |
| 80 |
doc("Websocket connections are not limited. The connections " |
| 81 |
"are removed from the count after the handshake completes."), |
| 82 |
case os:type() of |
| 83 |
{win32, _} -> |
| 84 |
{skip, "Tests that use too many sockets are disabled on Windows " |
| 85 |
"to prevent intermittent failures."}; |
| 86 |
{unix, _} -> |
| 87 |
case list_to_integer(os:cmd("printf `ulimit -n`")) of |
| 88 |
Limit when Limit > 6100 -> |
| 89 |
do_unlimited_connections(Config); |
| 90 |
_ -> |
| 91 |
{skip, "`ulimit -n` reports a limit too low for this test."} |
| 92 |
end |
| 93 |
end. |
| 94 |
|
| 95 |
do_unlimited_connections(Config) -> |
| 96 |
_ = [begin |
| 97 |
spawn_link(fun() -> do_connect_and_loop(Config) end), |
| 98 |
timer:sleep(1) |
| 99 |
end || _ <- lists:seq(1, 3000)], |
| 100 |
timer:sleep(1000), |
| 101 |
%% We have at least 3000 client and 3000 server sockets. |
| 102 |
true = length(erlang:ports()) > 6000, |
| 103 |
%% Ranch thinks we have no connections. |
| 104 |
0 = ranch_server:count_connections(ws), |
| 105 |
ok. |
| 106 |
|
| 107 |
do_connect_and_loop(Config) -> |
| 108 |
{ok, Socket, _} = do_handshake("/ws_echo", Config), |
| 109 |
do_loop(Socket). |
| 110 |
|
| 111 |
do_loop(Socket) -> |
| 112 |
%% Masked text hello echoed back clear by the server. |
| 113 |
Mask = 16#37fa213d, |
| 114 |
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>), |
| 115 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>), |
| 116 |
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 117 |
timer:sleep(1000), |
| 118 |
do_loop(Socket). |
| 119 |
|
| 120 |
ws0(Config) -> |
| 121 |
doc("Websocket version 0 (hixie-76 draft) is no longer supported."), |
| 122 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]), |
| 123 |
ok = gen_tcp:send(Socket, |
| 124 |
"GET /ws_echo_timer HTTP/1.1\r\n" |
| 125 |
"Host: localhost\r\n" |
| 126 |
"Connection: Upgrade\r\n" |
| 127 |
"Upgrade: WebSocket\r\n" |
| 128 |
"Origin: http://localhost\r\n" |
| 129 |
"Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n" |
| 130 |
"Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n" |
| 131 |
"\r\n"), |
| 132 |
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), |
| 133 |
{ok, {http_response, {1, 1}, 400, _}, _} = erlang:decode_packet(http, Handshake, []), |
| 134 |
ok. |
| 135 |
|
| 136 |
ws7(Config) -> |
| 137 |
doc("Websocket version 7 (draft) is supported."), |
| 138 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]), |
| 139 |
ok = gen_tcp:send(Socket, [ |
| 140 |
"GET /ws_echo_timer HTTP/1.1\r\n" |
| 141 |
"Host: localhost\r\n" |
| 142 |
"Connection: Upgrade\r\n" |
| 143 |
"Upgrade: websocket\r\n" |
| 144 |
"Sec-WebSocket-Origin: http://localhost\r\n" |
| 145 |
"Sec-WebSocket-Version: 7\r\n" |
| 146 |
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 147 |
"\r\n"]), |
| 148 |
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), |
| 149 |
{ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []), |
| 150 |
[Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []), |
| 151 |
{_, "Upgrade"} = lists:keyfind('Connection', 1, Headers), |
| 152 |
{_, "websocket"} = lists:keyfind('Upgrade', 1, Headers), |
| 153 |
{_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers), |
| 154 |
do_ws_version(Socket). |
| 155 |
|
| 156 |
ws8(Config) -> |
| 157 |
doc("Websocket version 8 (draft) is supported."), |
| 158 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]), |
| 159 |
ok = gen_tcp:send(Socket, [ |
| 160 |
"GET /ws_echo_timer HTTP/1.1\r\n" |
| 161 |
"Host: localhost\r\n" |
| 162 |
"Connection: Upgrade\r\n" |
| 163 |
"Upgrade: websocket\r\n" |
| 164 |
"Sec-WebSocket-Origin: http://localhost\r\n" |
| 165 |
"Sec-WebSocket-Version: 8\r\n" |
| 166 |
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 167 |
"\r\n"]), |
| 168 |
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), |
| 169 |
{ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []), |
| 170 |
[Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []), |
| 171 |
{_, "Upgrade"} = lists:keyfind('Connection', 1, Headers), |
| 172 |
{_, "websocket"} = lists:keyfind('Upgrade', 1, Headers), |
| 173 |
{_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers), |
| 174 |
do_ws_version(Socket). |
| 175 |
|
| 176 |
ws13(Config) -> |
| 177 |
doc("Websocket version 13 (RFC) is supported."), |
| 178 |
{ok, Socket, _} = do_handshake("/ws_echo_timer", Config), |
| 179 |
do_ws_version(Socket). |
| 180 |
|
| 181 |
do_ws_version(Socket) -> |
| 182 |
%% Masked text hello echoed back clear by the server. |
| 183 |
Mask = 16#37fa213d, |
| 184 |
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>), |
| 185 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>), |
| 186 |
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 187 |
%% Empty binary frame echoed back. |
| 188 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 0:7, 0:32 >>), |
| 189 |
{ok, << 1:1, 0:3, 2:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 190 |
%% Masked binary hello echoed back clear by the server. |
| 191 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>), |
| 192 |
{ok, << 1:1, 0:3, 2:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 193 |
%% Frames sent on timer by the handler. |
| 194 |
{ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 195 |
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 196 |
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 197 |
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 198 |
%% Client-initiated ping/pong. |
| 199 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>), |
| 200 |
{ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 201 |
%% Client-initiated close. |
| 202 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), |
| 203 |
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 204 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 205 |
ok. |
| 206 |
|
| 207 |
ws_deflate_max_frame_size_close(Config) -> |
| 208 |
doc("Server closes connection when decompressed frame size exceeds max_frame_size option"), |
| 209 |
%% max_frame_size is set to 8 bytes in ws_max_frame_size. |
| 210 |
{ok, Socket, Headers} = do_handshake("/ws_max_frame_size", |
| 211 |
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config), |
| 212 |
{_, "permessage-deflate"} = lists:keyfind("sec-websocket-extensions", 1, Headers), |
| 213 |
Mask = 16#11223344, |
| 214 |
Z = zlib:open(), |
| 215 |
zlib:deflateInit(Z, best_compression, deflated, -15, 8, default), |
| 216 |
CompressedData0 = iolist_to_binary(zlib:deflate(Z, <<0:800>>, sync)), |
| 217 |
CompressedData = binary:part(CompressedData0, 0, byte_size(CompressedData0) - 4), |
| 218 |
MaskedData = do_mask(CompressedData, Mask, <<>>), |
| 219 |
Len = byte_size(MaskedData), |
| 220 |
true = Len < 8, |
| 221 |
ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, Len:7, Mask:32, MaskedData/binary >>), |
| 222 |
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 223 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 224 |
ok. |
| 225 |
|
| 226 |
ws_deflate_max_frame_size_chunked_close(Config) -> |
| 227 |
doc("Server closes connection when decompressed frame size exceeds " |
| 228 |
"max_frame_size option, even when the compressed payload is " |
| 229 |
"delivered over multiple separate TCP segments."), |
| 230 |
%% max_frame_size is set to 100 bytes in ws_max_frame_size_deflate. |
| 231 |
{ok, Socket, Headers} = do_handshake("/ws_max_frame_size_deflate", |
| 232 |
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config), |
| 233 |
{_, "permessage-deflate"} = lists:keyfind("sec-websocket-extensions", 1, Headers), |
| 234 |
Mask = 16#11223344, |
| 235 |
Z = zlib:open(), |
| 236 |
zlib:deflateInit(Z, best_compression, deflated, -15, 8, default), |
| 237 |
%% Three independently sync-flushed chunks, each decompressing to |
| 238 |
%% exactly 50 bytes. After the first two (100 bytes, exactly at |
| 239 |
%% max_frame_size) the server must not close yet; after the third |
| 240 |
%% (150 bytes, over max_frame_size) it must close with code 1009, |
| 241 |
%% regardless of the fact that each individual chunk's own wire |
| 242 |
%% size (~8-10 bytes) never looks oversized by itself. |
| 243 |
[Chunk1, Chunk2, Chunk3] = [iolist_to_binary(zlib:deflate(Z, <<0:400>>, sync)) |
| 244 |
|| _ <- lists:seq(1, 3)], |
| 245 |
Chunk1Size = byte_size(Chunk1), |
| 246 |
Chunk2Size = byte_size(Chunk2), |
| 247 |
CompressedData0 = iolist_to_binary([Chunk1, Chunk2, Chunk3]), |
| 248 |
CompressedData = binary:part(CompressedData0, 0, byte_size(CompressedData0) - 4), |
| 249 |
MaskedData = do_mask(CompressedData, Mask, <<>>), |
| 250 |
Len = byte_size(MaskedData), |
| 251 |
true = Len < 100, |
| 252 |
<< MaskedChunk1:Chunk1Size/binary, MaskedChunk2:Chunk2Size/binary, |
| 253 |
MaskedChunk3/binary >> = MaskedData, |
| 254 |
ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, Len:7, Mask:32 >>), |
| 255 |
ok = gen_tcp:send(Socket, MaskedChunk1), |
| 256 |
ok = gen_tcp:send(Socket, MaskedChunk2), |
| 257 |
%% 100 bytes decompressed so far: exactly at the limit, connection |
| 258 |
%% must still be open. |
| 259 |
{error, timeout} = gen_tcp:recv(Socket, 0, 200), |
| 260 |
ok = gen_tcp:send(Socket, MaskedChunk3), |
| 261 |
%% 150 bytes decompressed: over the limit, connection must close now. |
| 262 |
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 263 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 264 |
ok. |
| 265 |
|
| 266 |
ws_deflate_opts_client_context_takeover(Config) -> |
| 267 |
doc("Handler is configured with client context takeover enabled."), |
| 268 |
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?client_context_takeover", |
| 269 |
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config), |
| 270 |
{_, "permessage-deflate"} |
| 271 |
= lists:keyfind("sec-websocket-extensions", 1, Headers1), |
| 272 |
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?client_context_takeover", |
| 273 |
"Sec-WebSocket-Extensions: permessage-deflate; client_no_context_takeover\r\n", Config), |
| 274 |
{_, "permessage-deflate; client_no_context_takeover"} |
| 275 |
= lists:keyfind("sec-websocket-extensions", 1, Headers2), |
| 276 |
ok. |
| 277 |
|
| 278 |
ws_deflate_opts_client_no_context_takeover(Config) -> |
| 279 |
doc("Handler is configured with client context takeover disabled."), |
| 280 |
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?client_no_context_takeover", |
| 281 |
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config), |
| 282 |
{_, "permessage-deflate; client_no_context_takeover"} |
| 283 |
= lists:keyfind("sec-websocket-extensions", 1, Headers1), |
| 284 |
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?client_no_context_takeover", |
| 285 |
"Sec-WebSocket-Extensions: permessage-deflate; client_no_context_takeover\r\n", Config), |
| 286 |
{_, "permessage-deflate; client_no_context_takeover"} |
| 287 |
= lists:keyfind("sec-websocket-extensions", 1, Headers2), |
| 288 |
ok. |
| 289 |
|
| 290 |
%% We must send client_max_window_bits to indicate we support it. |
| 291 |
ws_deflate_opts_client_max_window_bits(Config) -> |
| 292 |
doc("Handler is configured with client max window bits."), |
| 293 |
{ok, _, Headers} = do_handshake("/ws_deflate_opts?client_max_window_bits", |
| 294 |
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n", Config), |
| 295 |
{_, "permessage-deflate; client_max_window_bits=9"} |
| 296 |
= lists:keyfind("sec-websocket-extensions", 1, Headers), |
| 297 |
ok. |
| 298 |
|
| 299 |
ws_deflate_opts_client_max_window_bits_override(Config) -> |
| 300 |
doc("Handler is configured with client max window bits."), |
| 301 |
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?client_max_window_bits", |
| 302 |
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits=8\r\n", Config), |
| 303 |
{_, "permessage-deflate; client_max_window_bits=8"} |
| 304 |
= lists:keyfind("sec-websocket-extensions", 1, Headers1), |
| 305 |
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?client_max_window_bits", |
| 306 |
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits=12\r\n", Config), |
| 307 |
{_, "permessage-deflate; client_max_window_bits=9"} |
| 308 |
= lists:keyfind("sec-websocket-extensions", 1, Headers2), |
| 309 |
ok. |
| 310 |
|
| 311 |
%% @todo This might be better in an rfc7692_SUITE. |
| 312 |
%% |
| 313 |
%% 7.1.2.2 |
| 314 |
%% If a received extension negotiation offer doesn't have the |
| 315 |
%% "client_max_window_bits" extension parameter, the corresponding |
| 316 |
%% extension negotiation response to the offer MUST NOT include the |
| 317 |
%% "client_max_window_bits" extension parameter. |
| 318 |
ws_deflate_opts_client_max_window_bits_only_in_server(Config) -> |
| 319 |
doc("Handler is configured with non-default client max window bits but " |
| 320 |
"client doesn't send the parameter; compression is disabled."), |
| 321 |
{ok, _, Headers} = do_handshake("/ws_deflate_opts?client_max_window_bits", |
| 322 |
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config), |
| 323 |
false = lists:keyfind("sec-websocket-extensions", 1, Headers), |
| 324 |
ok. |
| 325 |
|
| 326 |
ws_deflate_opts_server_context_takeover(Config) -> |
| 327 |
doc("Handler is configured with server context takeover enabled."), |
| 328 |
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?server_context_takeover", |
| 329 |
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config), |
| 330 |
{_, "permessage-deflate"} |
| 331 |
= lists:keyfind("sec-websocket-extensions", 1, Headers1), |
| 332 |
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?server_context_takeover", |
| 333 |
"Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover\r\n", Config), |
| 334 |
{_, "permessage-deflate; server_no_context_takeover"} |
| 335 |
= lists:keyfind("sec-websocket-extensions", 1, Headers2), |
| 336 |
ok. |
| 337 |
|
| 338 |
ws_deflate_opts_server_no_context_takeover(Config) -> |
| 339 |
doc("Handler is configured with server context takeover disabled."), |
| 340 |
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?server_no_context_takeover", |
| 341 |
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config), |
| 342 |
{_, "permessage-deflate; server_no_context_takeover"} |
| 343 |
= lists:keyfind("sec-websocket-extensions", 1, Headers1), |
| 344 |
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?server_no_context_takeover", |
| 345 |
"Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover\r\n", Config), |
| 346 |
{_, "permessage-deflate; server_no_context_takeover"} |
| 347 |
= lists:keyfind("sec-websocket-extensions", 1, Headers2), |
| 348 |
ok. |
| 349 |
|
| 350 |
ws_deflate_opts_server_max_window_bits(Config) -> |
| 351 |
doc("Handler is configured with server max window bits."), |
| 352 |
{ok, _, Headers} = do_handshake("/ws_deflate_opts?server_max_window_bits", |
| 353 |
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config), |
| 354 |
{_, "permessage-deflate; server_max_window_bits=9"} |
| 355 |
= lists:keyfind("sec-websocket-extensions", 1, Headers), |
| 356 |
ok. |
| 357 |
|
| 358 |
ws_deflate_opts_server_max_window_bits_override(Config) -> |
| 359 |
doc("Handler is configured with server max window bits."), |
| 360 |
{ok, _, Headers1} = do_handshake("/ws_deflate_opts?server_max_window_bits", |
| 361 |
"Sec-WebSocket-Extensions: permessage-deflate; server_max_window_bits=8\r\n", Config), |
| 362 |
{_, "permessage-deflate; server_max_window_bits=8"} |
| 363 |
= lists:keyfind("sec-websocket-extensions", 1, Headers1), |
| 364 |
{ok, _, Headers2} = do_handshake("/ws_deflate_opts?server_max_window_bits", |
| 365 |
"Sec-WebSocket-Extensions: permessage-deflate; server_max_window_bits=12\r\n", Config), |
| 366 |
{_, "permessage-deflate; server_max_window_bits=9"} |
| 367 |
= lists:keyfind("sec-websocket-extensions", 1, Headers2), |
| 368 |
ok. |
| 369 |
|
| 370 |
ws_deflate_opts_zlevel(Config) -> |
| 371 |
doc("Handler is configured with zlib level."), |
| 372 |
do_ws_deflate_opts_z("/ws_deflate_opts?level", Config). |
| 373 |
|
| 374 |
ws_deflate_opts_zmemlevel(Config) -> |
| 375 |
doc("Handler is configured with zlib mem_level."), |
| 376 |
do_ws_deflate_opts_z("/ws_deflate_opts?mem_level", Config). |
| 377 |
|
| 378 |
ws_deflate_opts_zstrategy(Config) -> |
| 379 |
doc("Handler is configured with zlib strategy."), |
| 380 |
do_ws_deflate_opts_z("/ws_deflate_opts?strategy", Config). |
| 381 |
|
| 382 |
do_ws_deflate_opts_z(Path, Config) -> |
| 383 |
{ok, Socket, Headers} = do_handshake(Path, |
| 384 |
"Sec-WebSocket-Extensions: permessage-deflate\r\n", Config), |
| 385 |
{_, "permessage-deflate"} = lists:keyfind("sec-websocket-extensions", 1, Headers), |
| 386 |
%% Send and receive a compressed "Hello" frame. |
| 387 |
Mask = 16#11223344, |
| 388 |
CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>, |
| 389 |
MaskedHello = do_mask(CompressedHello, Mask, <<>>), |
| 390 |
ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary >>), |
| 391 |
{ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000), |
| 392 |
%% Client-initiated close. |
| 393 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), |
| 394 |
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 395 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 396 |
ok. |
| 397 |
|
| 398 |
ws_dont_validate_utf8(Config) -> |
| 399 |
doc("Handler is configured with UTF-8 validation disabled."), |
| 400 |
{ok, Socket, _} = do_handshake("/ws_dont_validate_utf8", Config), |
| 401 |
%% Send an invalid UTF-8 text frame and receive it back. |
| 402 |
Mask = 16#37fa213d, |
| 403 |
MaskedInvalid = do_mask(<<255, 255, 255, 255>>, Mask, <<>>), |
| 404 |
ok = gen_tcp:send(Socket, <<1:1, 0:3, 1:4, 1:1, 4:7, Mask:32, MaskedInvalid/binary>>), |
| 405 |
{ok, <<1:1, 0:3, 1:4, 0:1, 4:7, 255, 255, 255, 255>>} = gen_tcp:recv(Socket, 0, 6000), |
| 406 |
ok. |
| 407 |
|
| 408 |
ws_first_frame_with_handshake(Config) -> |
| 409 |
doc("Client sends the first frame immediately with the handshake. " |
| 410 |
"This is invalid according to the protocol but we still want " |
| 411 |
"to accept it if the handshake is successful."), |
| 412 |
Mask = 16#37fa213d, |
| 413 |
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>), |
| 414 |
{ok, Socket, _} = do_handshake("/ws_echo", "", |
| 415 |
<<1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary>>, |
| 416 |
Config), |
| 417 |
{ok, <<1:1, 0:3, 1:4, 0:1, 5:7, "Hello">>} = gen_tcp:recv(Socket, 0, 6000), |
| 418 |
ok. |
| 419 |
|
| 420 |
%% @todo Move these tests to ws_handler_SUITE. |
| 421 |
ws_init_return_ok(Config) -> |
| 422 |
doc("Handler does nothing."), |
| 423 |
{ok, Socket, _} = do_handshake("/ws_init?ok", Config), |
| 424 |
%% The handler does nothing; nothing should happen here. |
| 425 |
{error, timeout} = gen_tcp:recv(Socket, 0, 1000), |
| 426 |
ok. |
| 427 |
|
| 428 |
ws_init_return_ok_hibernate(Config) -> |
| 429 |
doc("Handler does nothing; hibernates."), |
| 430 |
{ok, Socket, _} = do_handshake("/ws_init?ok_hibernate", Config), |
| 431 |
%% The handler does nothing; nothing should happen here. |
| 432 |
{error, timeout} = gen_tcp:recv(Socket, 0, 1000), |
| 433 |
ok. |
| 434 |
|
| 435 |
ws_init_return_reply(Config) -> |
| 436 |
doc("Handler sends a text frame just after the handshake."), |
| 437 |
{ok, Socket, _} = do_handshake("/ws_init?reply", Config), |
| 438 |
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 439 |
ok. |
| 440 |
|
| 441 |
ws_init_return_reply_hibernate(Config) -> |
| 442 |
doc("Handler sends a text frame just after the handshake and then hibernates."), |
| 443 |
{ok, Socket, _} = do_handshake("/ws_init?reply_hibernate", Config), |
| 444 |
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 445 |
ok. |
| 446 |
|
| 447 |
ws_init_return_reply_close(Config) -> |
| 448 |
doc("Handler closes immediately after the handshake."), |
| 449 |
{ok, Socket, _} = do_handshake("/ws_init?reply_close", Config), |
| 450 |
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 451 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 452 |
ok. |
| 453 |
|
| 454 |
ws_init_return_reply_close_hibernate(Config) -> |
| 455 |
doc("Handler closes immediately after the handshake, then attempts to hibernate."), |
| 456 |
{ok, Socket, _} = do_handshake("/ws_init?reply_close_hibernate", Config), |
| 457 |
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 458 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 459 |
ok. |
| 460 |
|
| 461 |
ws_init_return_reply_many(Config) -> |
| 462 |
doc("Handler sends many frames just after the handshake."), |
| 463 |
{ok, Socket, _} = do_handshake("/ws_init?reply_many", Config), |
| 464 |
%% We catch all frames at once and check them directly. |
| 465 |
{ok, << |
| 466 |
1:1, 0:3, 1:4, 0:1, 5:7, "Hello", |
| 467 |
1:1, 0:3, 2:4, 0:1, 5:7, "World" >>} = gen_tcp:recv(Socket, 14, 6000), |
| 468 |
ok. |
| 469 |
|
| 470 |
ws_init_return_reply_many_hibernate(Config) -> |
| 471 |
doc("Handler sends many frames just after the handshake and then hibernates."), |
| 472 |
{ok, Socket, _} = do_handshake("/ws_init?reply_many_hibernate", Config), |
| 473 |
%% We catch all frames at once and check them directly. |
| 474 |
{ok, << |
| 475 |
1:1, 0:3, 1:4, 0:1, 5:7, "Hello", |
| 476 |
1:1, 0:3, 2:4, 0:1, 5:7, "World" >>} = gen_tcp:recv(Socket, 14, 6000), |
| 477 |
ok. |
| 478 |
|
| 479 |
ws_init_return_reply_many_close(Config) -> |
| 480 |
doc("Handler sends many frames including a close frame just after the handshake."), |
| 481 |
{ok, Socket, _} = do_handshake("/ws_init?reply_many_close", Config), |
| 482 |
%% We catch all frames at once and check them directly. |
| 483 |
{ok, << |
| 484 |
1:1, 0:3, 1:4, 0:1, 5:7, "Hello", |
| 485 |
1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 9, 6000), |
| 486 |
ok. |
| 487 |
|
| 488 |
ws_init_return_reply_many_close_hibernate(Config) -> |
| 489 |
doc("Handler sends many frames including a close frame just after the handshake and then hibernates."), |
| 490 |
{ok, Socket, _} = do_handshake("/ws_init?reply_many_close_hibernate", Config), |
| 491 |
%% We catch all frames at once and check them directly. |
| 492 |
{ok, << |
| 493 |
1:1, 0:3, 1:4, 0:1, 5:7, "Hello", |
| 494 |
1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 9, 6000), |
| 495 |
ok. |
| 496 |
|
| 497 |
ws_init_shutdown_before_handshake(Config) -> |
| 498 |
doc("Handler stops before Websocket handshake."), |
| 499 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]), |
| 500 |
ok = gen_tcp:send(Socket, [ |
| 501 |
"GET /ws_init_shutdown HTTP/1.1\r\n" |
| 502 |
"Host: localhost\r\n" |
| 503 |
"Connection: Upgrade\r\n" |
| 504 |
"Origin: http://localhost\r\n" |
| 505 |
"Sec-WebSocket-Version: 13\r\n" |
| 506 |
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 507 |
"Upgrade: websocket\r\n" |
| 508 |
"\r\n"]), |
| 509 |
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), |
| 510 |
{ok, {http_response, {1, 1}, 403, _}, _Rest} = erlang:decode_packet(http, Handshake, []), |
| 511 |
ok. |
| 512 |
|
| 513 |
ws_max_frame_size_close(Config) -> |
| 514 |
doc("Server closes connection when frame size exceeds max_frame_size option"), |
| 515 |
%% max_frame_size is set to 8 bytes in ws_max_frame_size. |
| 516 |
{ok, Socket, _} = do_handshake("/ws_max_frame_size", Config), |
| 517 |
Mask = 16#11223344, |
| 518 |
MaskedHello = do_mask(<<"HelloHello">>, Mask, <<>>), |
| 519 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 10:7, Mask:32, MaskedHello/binary >>), |
| 520 |
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 521 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 522 |
ok. |
| 523 |
|
| 524 |
ws_max_frame_size_final_fragment_close(Config) -> |
| 525 |
doc("Server closes connection when final fragmented frame " |
| 526 |
"exceeds max_frame_size option"), |
| 527 |
%% max_frame_size is set to 8 bytes in ws_max_frame_size. |
| 528 |
{ok, Socket, _} = do_handshake("/ws_max_frame_size", Config), |
| 529 |
Mask = 16#11223344, |
| 530 |
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>), |
| 531 |
ok = gen_tcp:send(Socket, << 0:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>), |
| 532 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>), |
| 533 |
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 534 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 535 |
ok. |
| 536 |
|
| 537 |
ws_max_frame_size_intermediate_fragment_close(Config) -> |
| 538 |
doc("Server closes connection when intermediate fragmented frame " |
| 539 |
"exceeds max_frame_size option"), |
| 540 |
%% max_frame_size is set to 8 bytes in ws_max_frame_size. |
| 541 |
{ok, Socket, _} = do_handshake("/ws_max_frame_size", Config), |
| 542 |
Mask = 16#11223344, |
| 543 |
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>), |
| 544 |
ok = gen_tcp:send(Socket, << 0:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>), |
| 545 |
ok = gen_tcp:send(Socket, << 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>), |
| 546 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>), |
| 547 |
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 548 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 549 |
ok. |
| 550 |
|
| 551 |
ws_ping(Config) -> |
| 552 |
doc("Server initiated pings can receive a pong in response."), |
| 553 |
{ok, Socket, _} = do_handshake("/ws_ping", Config), |
| 554 |
%% Receive a server-sent ping. |
| 555 |
{ok, << 1:1, 0:3, 9:4, 0:1, 0:7 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 556 |
%% Send a pong back with a 0 mask. |
| 557 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 10:4, 1:1, 0:7, 0:32 >>), |
| 558 |
%% Receive a text frame as a result. |
| 559 |
{ok, << 1:1, 0:3, 1:4, 0:1, 4:7, "OK!!" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 560 |
ok. |
| 561 |
|
| 562 |
ws_send_close(Config) -> |
| 563 |
doc("Server-initiated close frame ends the connection."), |
| 564 |
{ok, Socket, _} = do_handshake("/ws_send_close", Config), |
| 565 |
%% We catch all frames at once and check them directly. |
| 566 |
{ok, << |
| 567 |
1:1, 0:3, 1:4, 0:1, 4:7, "send", |
| 568 |
1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 8, 6000), |
| 569 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 570 |
ok. |
| 571 |
|
| 572 |
ws_send_close_payload(Config) -> |
| 573 |
doc("Server-initiated close frame with payload ends the connection."), |
| 574 |
{ok, Socket, _} = do_handshake("/ws_send_close_payload", Config), |
| 575 |
%% We catch all frames at once and check them directly. |
| 576 |
{ok, << |
| 577 |
1:1, 0:3, 1:4, 0:1, 4:7, "send", |
| 578 |
1:1, 0:3, 8:4, 0:1, 12:7, 1001:16, "some text!" >>} = gen_tcp:recv(Socket, 20, 6000), |
| 579 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 580 |
ok. |
| 581 |
|
| 582 |
ws_send_many(Config) -> |
| 583 |
doc("Server sends many frames in a single reply."), |
| 584 |
{ok, Socket, _} = do_handshake("/ws_send_many", Config), |
| 585 |
%% We catch all frames at once and check them directly. |
| 586 |
{ok, << |
| 587 |
1:1, 0:3, 1:4, 0:1, 3:7, "one", |
| 588 |
1:1, 0:3, 1:4, 0:1, 3:7, "two", |
| 589 |
1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >>} = gen_tcp:recv(Socket, 18, 6000), |
| 590 |
ok. |
| 591 |
|
| 592 |
ws_single_bytes(Config) -> |
| 593 |
doc("Client sends a text frame one byte at a time."), |
| 594 |
{ok, Socket, _} = do_handshake("/ws_echo", Config), |
| 595 |
%% We sleep between sends to make sure only one byte is sent. |
| 596 |
ok = gen_tcp:send(Socket, << 16#81 >>), timer:sleep(100), |
| 597 |
ok = gen_tcp:send(Socket, << 16#85 >>), timer:sleep(100), |
| 598 |
ok = gen_tcp:send(Socket, << 16#37 >>), timer:sleep(100), |
| 599 |
ok = gen_tcp:send(Socket, << 16#fa >>), timer:sleep(100), |
| 600 |
ok = gen_tcp:send(Socket, << 16#21 >>), timer:sleep(100), |
| 601 |
ok = gen_tcp:send(Socket, << 16#3d >>), timer:sleep(100), |
| 602 |
ok = gen_tcp:send(Socket, << 16#7f >>), timer:sleep(100), |
| 603 |
ok = gen_tcp:send(Socket, << 16#9f >>), timer:sleep(100), |
| 604 |
ok = gen_tcp:send(Socket, << 16#4d >>), timer:sleep(100), |
| 605 |
ok = gen_tcp:send(Socket, << 16#51 >>), timer:sleep(100), |
| 606 |
ok = gen_tcp:send(Socket, << 16#58 >>), |
| 607 |
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 608 |
ok. |
| 609 |
|
| 610 |
ws_subprotocol(Config) -> |
| 611 |
doc("Websocket sub-protocol negotiation."), |
| 612 |
{ok, _, Headers} = do_handshake("/ws_subprotocol", |
| 613 |
"Sec-WebSocket-Protocol: foo, bar\r\n", Config), |
| 614 |
{_, "foo"} = lists:keyfind("sec-websocket-protocol", 1, Headers), |
| 615 |
ok. |
| 616 |
|
| 617 |
ws_text_fragments(Config) -> |
| 618 |
doc("Client sends fragmented text frames."), |
| 619 |
{ok, Socket, _} = do_handshake("/ws_echo", Config), |
| 620 |
%% Send two "Hello" over two fragments and two sends. |
| 621 |
Mask = 16#37fa213d, |
| 622 |
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>), |
| 623 |
ok = gen_tcp:send(Socket, << 0:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>), |
| 624 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>), |
| 625 |
{ok, << 1:1, 0:3, 1:4, 0:1, 10:7, "HelloHello" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 626 |
%% Send three "Hello" over three fragments and one send. |
| 627 |
ok = gen_tcp:send(Socket, [ |
| 628 |
<< 0:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>, |
| 629 |
<< 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>, |
| 630 |
<< 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>]), |
| 631 |
{ok, << 1:1, 0:3, 1:4, 0:1, 15:7, "HelloHelloHello" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 632 |
ok. |
| 633 |
|
| 634 |
ws_timeout_hibernate(Config) -> |
| 635 |
doc("Server-initiated close on timeout with hibernating process."), |
| 636 |
{ok, Socket, _} = do_handshake("/ws_timeout_hibernate", Config), |
| 637 |
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 638 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 639 |
ok. |
| 640 |
|
| 641 |
ws_timeout_no_cancel(Config) -> |
| 642 |
doc("Server-initiated timeout is not influenced by reception of Erlang messages."), |
| 643 |
{ok, Socket, _} = do_handshake("/ws_timeout_cancel", Config), |
| 644 |
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 645 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 646 |
ok. |
| 647 |
|
| 648 |
ws_timeout_reset(Config) -> |
| 649 |
doc("Server-initiated timeout is reset when client sends more data."), |
| 650 |
{ok, Socket, _} = do_handshake("/ws_timeout_cancel", Config), |
| 651 |
%% Send and receive back a frame a few times. |
| 652 |
Mask = 16#37fa213d, |
| 653 |
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>), |
| 654 |
[begin |
| 655 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>), |
| 656 |
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000), |
| 657 |
timer:sleep(500) |
| 658 |
end || _ <- [1, 2, 3, 4]], |
| 659 |
%% Timeout will occur after we stop sending data. |
| 660 |
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 661 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 662 |
ok. |
| 663 |
|
| 664 |
ws_webkit_deflate(Config) -> |
| 665 |
doc("x-webkit-deflate-frame compression."), |
| 666 |
{ok, Socket, Headers} = do_handshake("/ws_echo", |
| 667 |
"Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config), |
| 668 |
{_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers), |
| 669 |
%% Send and receive a compressed "Hello" frame. |
| 670 |
Mask = 16#11223344, |
| 671 |
CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>, |
| 672 |
MaskedHello = do_mask(CompressedHello, Mask, <<>>), |
| 673 |
ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary >>), |
| 674 |
{ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000), |
| 675 |
%% Client-initiated close. |
| 676 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), |
| 677 |
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 678 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 679 |
ok. |
| 680 |
|
| 681 |
ws_webkit_deflate_fragments(Config) -> |
| 682 |
doc("Client sends an x-webkit-deflate-frame compressed and fragmented text frame."), |
| 683 |
{ok, Socket, Headers} = do_handshake("/ws_echo", |
| 684 |
"Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config), |
| 685 |
{_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers), |
| 686 |
%% Send a compressed "Hello" over two fragments and two sends. |
| 687 |
Mask = 16#11223344, |
| 688 |
CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>, |
| 689 |
MaskedHello1 = do_mask(binary:part(CompressedHello, 0, 4), Mask, <<>>), |
| 690 |
MaskedHello2 = do_mask(binary:part(CompressedHello, 4, 3), Mask, <<>>), |
| 691 |
ok = gen_tcp:send(Socket, << 0:1, 1:1, 0:2, 1:4, 1:1, 4:7, Mask:32, MaskedHello1/binary >>), |
| 692 |
ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 0:4, 1:1, 3:7, Mask:32, MaskedHello2/binary >>), |
| 693 |
{ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000), |
| 694 |
ok. |
| 695 |
|
| 696 |
ws_webkit_deflate_single_bytes(Config) -> |
| 697 |
doc("Client sends an x-webkit-deflate-frame compressed text frame one byte at a time."), |
| 698 |
{ok, Socket, Headers} = do_handshake("/ws_echo", |
| 699 |
"Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config), |
| 700 |
{_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers), |
| 701 |
%% We sleep between sends to make sure only one byte is sent. |
| 702 |
Mask = 16#11223344, |
| 703 |
CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>, |
| 704 |
MaskedHello = do_mask(CompressedHello, Mask, <<>>), |
| 705 |
ok = gen_tcp:send(Socket, << 16#c1 >>), timer:sleep(100), |
| 706 |
ok = gen_tcp:send(Socket, << 16#87 >>), timer:sleep(100), |
| 707 |
ok = gen_tcp:send(Socket, << 16#11 >>), timer:sleep(100), |
| 708 |
ok = gen_tcp:send(Socket, << 16#22 >>), timer:sleep(100), |
| 709 |
ok = gen_tcp:send(Socket, << 16#33 >>), timer:sleep(100), |
| 710 |
ok = gen_tcp:send(Socket, << 16#44 >>), timer:sleep(100), |
| 711 |
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 0)]), timer:sleep(100), |
| 712 |
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 1)]), timer:sleep(100), |
| 713 |
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 2)]), timer:sleep(100), |
| 714 |
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 3)]), timer:sleep(100), |
| 715 |
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 4)]), timer:sleep(100), |
| 716 |
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 5)]), timer:sleep(100), |
| 717 |
ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 6)]), |
| 718 |
{ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000), |
| 719 |
ok. |
| 720 |
|
| 721 |
%% Internal. |
| 722 |
|
| 723 |
do_handshake(Path, Config) -> |
| 724 |
do_handshake(Path, "", "", Config). |
| 725 |
|
| 726 |
do_handshake(Path, ExtraHeaders, Config) -> |
| 727 |
do_handshake(Path, ExtraHeaders, "", Config). |
| 728 |
|
| 729 |
do_handshake(Path, ExtraHeaders, ExtraData, Config) -> |
| 730 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), |
| 731 |
[binary, {active, false}]), |
| 732 |
ok = gen_tcp:send(Socket, [ |
| 733 |
"GET ", Path, " HTTP/1.1\r\n" |
| 734 |
"Host: localhost\r\n" |
| 735 |
"Connection: Upgrade\r\n" |
| 736 |
"Origin: http://localhost\r\n" |
| 737 |
"Sec-WebSocket-Version: 13\r\n" |
| 738 |
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 739 |
"Upgrade: websocket\r\n", |
| 740 |
ExtraHeaders, |
| 741 |
"\r\n", |
| 742 |
ExtraData]), |
| 743 |
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), |
| 744 |
{ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []), |
| 745 |
[Headers, Data] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []), |
| 746 |
%% Queue extra data back, if any. We don't want to receive it yet. |
| 747 |
case Data of |
| 748 |
<<>> -> ok; |
| 749 |
_ -> gen_tcp:unrecv(Socket, Data) |
| 750 |
end, |
| 751 |
{_, "Upgrade"} = lists:keyfind('Connection', 1, Headers), |
| 752 |
{_, "websocket"} = lists:keyfind('Upgrade', 1, Headers), |
| 753 |
{_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers), |
| 754 |
{ok, Socket, Headers}. |
| 755 |
|
| 756 |
do_decode_headers({ok, http_eoh, Rest}, Acc) -> |
| 757 |
[Acc, Rest]; |
| 758 |
do_decode_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) -> |
| 759 |
F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end, |
| 760 |
do_decode_headers(erlang:decode_packet(httph, Rest, []), [{F(Key), Value}|Acc]). |
| 761 |
|
| 762 |
do_mask(<<>>, _, Acc) -> |
| 763 |
Acc; |
| 764 |
do_mask(<< O:32, Rest/bits >>, MaskKey, Acc) -> |
| 765 |
T = O bxor MaskKey, |
| 766 |
do_mask(Rest, MaskKey, << Acc/binary, T:32 >>); |
| 767 |
do_mask(<< O:24 >>, MaskKey, Acc) -> |
| 768 |
<< MaskKey2:24, _:8 >> = << MaskKey:32 >>, |
| 769 |
T = O bxor MaskKey2, |
| 770 |
<< Acc/binary, T:24 >>; |
| 771 |
do_mask(<< O:16 >>, MaskKey, Acc) -> |
| 772 |
<< MaskKey2:16, _:16 >> = << MaskKey:32 >>, |
| 773 |
T = O bxor MaskKey2, |
| 774 |
<< Acc/binary, T:16 >>; |
| 775 |
do_mask(<< O:8 >>, MaskKey, Acc) -> |
| 776 |
<< MaskKey2:8, _:24 >> = << MaskKey:32 >>, |
| 777 |
T = O bxor MaskKey2, |
| 778 |
<< Acc/binary, T:8 >>. |