| 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(http2_SUITE). |
| 16 |
-compile(export_all). |
| 17 |
-compile(nowarn_export_all). |
| 18 |
|
| 19 |
-import(ct_helper, [config/2]). |
| 20 |
-import(ct_helper, [doc/1]). |
| 21 |
-import(ct_helper, [get_remote_pid_tcp/1]). |
| 22 |
-import(cowboy_test, [gun_open/1]). |
| 23 |
|
| 24 |
all() -> [{group, clear}]. |
| 25 |
|
| 26 |
groups() -> [{clear, [parallel], ct_helper:all(?MODULE)}]. |
| 27 |
|
| 28 |
init_dispatch(_) -> |
| 29 |
cowboy_router:compile([{"localhost", [ |
| 30 |
{"/", hello_h, []}, |
| 31 |
{"/echo/:key", echo_h, []}, |
| 32 |
{"/resp_iolist_body", resp_iolist_body_h, []}, |
| 33 |
{"/streamed_result/:n/:interval", streamed_result_h, []}, |
| 34 |
{"/inform", resp_invalid_headers_h, []}, |
| 35 |
{"/reply", resp_invalid_headers_h, []}, |
| 36 |
{"/stream_reply", resp_invalid_headers_h, []}, |
| 37 |
{"/stream_trailers", resp_invalid_headers_h, []}, |
| 38 |
{"/switch_protocol", resp_invalid_headers_h, []}, |
| 39 |
{"/push", resp_invalid_headers_h, []} |
| 40 |
]}]). |
| 41 |
|
| 42 |
%% Do a prior knowledge handshake (function originally copied from rfc7540_SUITE). |
| 43 |
do_handshake(Config) -> |
| 44 |
do_handshake(#{}, Config). |
| 45 |
|
| 46 |
do_handshake(Settings, Config) -> |
| 47 |
{ok, Socket} = gen_tcp:connect("localhost", config(port, Config), |
| 48 |
[binary, {active, false}|proplists:get_value(tcp_opts, Config, [])]), |
| 49 |
%% Send a valid preface. |
| 50 |
ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(Settings)]), |
| 51 |
%% Receive the server preface. |
| 52 |
{ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000), |
| 53 |
{ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000), |
| 54 |
%% Send the SETTINGS ack. |
| 55 |
ok = gen_tcp:send(Socket, cow_http2:settings_ack()), |
| 56 |
%% Receive the SETTINGS ack. |
| 57 |
{ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000), |
| 58 |
{ok, Socket}. |
| 59 |
|
| 60 |
hibernate(Config) -> |
| 61 |
doc("Ensure that we can enable hibernation for HTTP/1.1 connections."), |
| 62 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 63 |
env => #{dispatch => init_dispatch(Config)}, |
| 64 |
hibernate => true |
| 65 |
}), |
| 66 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 67 |
try |
| 68 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 69 |
{ok, http2} = gun:await_up(ConnPid), |
| 70 |
StreamRef1 = gun:get(ConnPid, "/"), |
| 71 |
StreamRef2 = gun:get(ConnPid, "/"), |
| 72 |
StreamRef3 = gun:get(ConnPid, "/"), |
| 73 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef1), |
| 74 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef2), |
| 75 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef3), |
| 76 |
gun:close(ConnPid) |
| 77 |
after |
| 78 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 79 |
end. |
| 80 |
|
| 81 |
idle_timeout(Config) -> |
| 82 |
doc("Terminate when the idle timeout is reached."), |
| 83 |
ProtoOpts = #{ |
| 84 |
env => #{dispatch => init_dispatch(Config)}, |
| 85 |
idle_timeout => 1000 |
| 86 |
}, |
| 87 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 88 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 89 |
try |
| 90 |
{ok, Socket} = do_handshake([{port, Port}|Config]), |
| 91 |
timer:sleep(1000), |
| 92 |
%% Receive a GOAWAY frame back with NO_ERROR. |
| 93 |
{ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 1000), |
| 94 |
gen_tcp:close(Socket) |
| 95 |
after |
| 96 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 97 |
end. |
| 98 |
|
| 99 |
idle_timeout_infinity(Config) -> |
| 100 |
doc("Ensure the idle_timeout option accepts the infinity value."), |
| 101 |
ProtoOpts = #{ |
| 102 |
env => #{dispatch => init_dispatch(Config)}, |
| 103 |
idle_timeout => infinity |
| 104 |
}, |
| 105 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 106 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 107 |
try |
| 108 |
{ok, Socket} = do_handshake([{port, Port}|Config]), |
| 109 |
timer:sleep(1000), |
| 110 |
%% Don't receive a GOAWAY frame. |
| 111 |
{error, timeout} = gen_tcp:recv(Socket, 17, 1000), |
| 112 |
gen_tcp:close(Socket) |
| 113 |
after |
| 114 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 115 |
end. |
| 116 |
|
| 117 |
idle_timeout_reset_on_data(Config) -> |
| 118 |
doc("Terminate when the idle timeout is reached."), |
| 119 |
ProtoOpts = #{ |
| 120 |
env => #{dispatch => init_dispatch(Config)}, |
| 121 |
idle_timeout => 1000 |
| 122 |
}, |
| 123 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 124 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 125 |
try |
| 126 |
{ok, Socket} = do_handshake([{port, Port}|Config]), |
| 127 |
%% We wait a little, send a PING, receive a PING ack. |
| 128 |
{error, timeout} = gen_tcp:recv(Socket, 17, 500), |
| 129 |
ok = gen_tcp:send(Socket, cow_http2:ping(0)), |
| 130 |
{ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000), |
| 131 |
%% Again. |
| 132 |
{error, timeout} = gen_tcp:recv(Socket, 17, 500), |
| 133 |
ok = gen_tcp:send(Socket, cow_http2:ping(0)), |
| 134 |
{ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000), |
| 135 |
%% And one more time. |
| 136 |
{error, timeout} = gen_tcp:recv(Socket, 17, 500), |
| 137 |
ok = gen_tcp:send(Socket, cow_http2:ping(0)), |
| 138 |
{ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000), |
| 139 |
%% The connection goes away soon after we stop sending data. |
| 140 |
timer:sleep(1000), |
| 141 |
{ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 1000), |
| 142 |
gen_tcp:close(Socket) |
| 143 |
after |
| 144 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 145 |
end. |
| 146 |
|
| 147 |
idle_timeout_on_send(Config) -> |
| 148 |
doc("Ensure the idle timeout is not reset when sending (by default)."), |
| 149 |
http_SUITE:do_idle_timeout_on_send(Config, http2). |
| 150 |
|
| 151 |
idle_timeout_reset_on_send(Config) -> |
| 152 |
doc("Ensure the reset_idle_timeout_on_send results in the " |
| 153 |
"idle timeout resetting when sending ."), |
| 154 |
http_SUITE:do_idle_timeout_reset_on_send(Config, http2). |
| 155 |
|
| 156 |
inactivity_timeout(Config) -> |
| 157 |
doc("Terminate when the inactivity timeout is reached."), |
| 158 |
ProtoOpts = #{ |
| 159 |
env => #{dispatch => init_dispatch(Config)}, |
| 160 |
inactivity_timeout => 1000 |
| 161 |
}, |
| 162 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 163 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 164 |
try |
| 165 |
{ok, Socket} = do_handshake([{port, Port}|Config]), |
| 166 |
receive after 1000 -> ok end, |
| 167 |
%% Receive a GOAWAY frame back with an INTERNAL_ERROR. |
| 168 |
{ok, << _:24, 7:8, _:72, 2:32 >>} = gen_tcp:recv(Socket, 17, 1000), |
| 169 |
gen_tcp:close(Socket) |
| 170 |
after |
| 171 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 172 |
end. |
| 173 |
|
| 174 |
initial_connection_window_size(Config) -> |
| 175 |
doc("Confirm a WINDOW_UPDATE frame is sent when the configured " |
| 176 |
"connection window is larger than the default."), |
| 177 |
ConfiguredSize = 100000, |
| 178 |
ProtoOpts = #{ |
| 179 |
env => #{dispatch => init_dispatch(Config)}, |
| 180 |
initial_connection_window_size => ConfiguredSize |
| 181 |
}, |
| 182 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 183 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 184 |
try |
| 185 |
{ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}]), |
| 186 |
%% Send a valid preface. |
| 187 |
ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]), |
| 188 |
%% Receive the server preface. |
| 189 |
{ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000), |
| 190 |
{ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000), |
| 191 |
%% Receive a WINDOW_UPDATE frame incrementing the connection window to 100000. |
| 192 |
{ok, <<4:24, 8:8, 0:41, Size:31>>} = gen_tcp:recv(Socket, 13, 1000), |
| 193 |
ConfiguredSize = Size + 65535, |
| 194 |
gen_tcp:close(Socket) |
| 195 |
after |
| 196 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 197 |
end. |
| 198 |
|
| 199 |
invalid_response_headers_inform(Config) -> |
| 200 |
doc("Ensure invalid response headers are rejected by default."), |
| 201 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 202 |
env => #{dispatch => init_dispatch(Config)} |
| 203 |
}), |
| 204 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 205 |
try |
| 206 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 207 |
{ok, http2} = gun:await_up(ConnPid), |
| 208 |
StreamRef = gun:get(ConnPid, "/inform"), |
| 209 |
{error,{stream_error,{stream_error,internal_error,_}}} |
| 210 |
= gun:await(ConnPid, StreamRef), |
| 211 |
gun:close(ConnPid) |
| 212 |
after |
| 213 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 214 |
end. |
| 215 |
|
| 216 |
invalid_response_headers_inform_ignore(Config) -> |
| 217 |
doc("Ensure invalid response headers are sent " |
| 218 |
"when allowed by configuration."), |
| 219 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 220 |
env => #{dispatch => init_dispatch(Config)}, |
| 221 |
invalid_response_headers => ignore |
| 222 |
}), |
| 223 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 224 |
try |
| 225 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 226 |
{ok, http2} = gun:await_up(ConnPid), |
| 227 |
StreamRef = gun:get(ConnPid, "/inform"), |
| 228 |
%% Gun rejects invalid response headers with a protocol_error stream error. |
| 229 |
{error,{stream_error,{stream_error,protocol_error,_}}} |
| 230 |
= gun:await(ConnPid, StreamRef), |
| 231 |
gun:close(ConnPid) |
| 232 |
after |
| 233 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 234 |
end. |
| 235 |
|
| 236 |
invalid_response_headers_reply(Config) -> |
| 237 |
doc("Ensure invalid response headers are rejected by default."), |
| 238 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 239 |
env => #{dispatch => init_dispatch(Config)} |
| 240 |
}), |
| 241 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 242 |
try |
| 243 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 244 |
{ok, http2} = gun:await_up(ConnPid), |
| 245 |
StreamRef = gun:get(ConnPid, "/reply"), |
| 246 |
{error,{stream_error,{stream_error,internal_error,_}}} |
| 247 |
= gun:await(ConnPid, StreamRef), |
| 248 |
gun:close(ConnPid) |
| 249 |
after |
| 250 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 251 |
end. |
| 252 |
|
| 253 |
invalid_response_headers_reply_ignore(Config) -> |
| 254 |
doc("Ensure invalid response headers are sent " |
| 255 |
"when allowed by configuration."), |
| 256 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 257 |
env => #{dispatch => init_dispatch(Config)}, |
| 258 |
invalid_response_headers => ignore |
| 259 |
}), |
| 260 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 261 |
try |
| 262 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 263 |
{ok, http2} = gun:await_up(ConnPid), |
| 264 |
StreamRef = gun:get(ConnPid, "/reply"), |
| 265 |
%% Gun rejects invalid response headers with a protocol_error stream error. |
| 266 |
{error,{stream_error,{stream_error,protocol_error,_}}} |
| 267 |
= gun:await(ConnPid, StreamRef), |
| 268 |
gun:close(ConnPid) |
| 269 |
after |
| 270 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 271 |
end. |
| 272 |
|
| 273 |
invalid_response_headers_stream_reply(Config) -> |
| 274 |
doc("Ensure invalid response headers are rejected by default."), |
| 275 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 276 |
env => #{dispatch => init_dispatch(Config)}, |
| 277 |
invalid_response_headers => error_terminate |
| 278 |
}), |
| 279 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 280 |
try |
| 281 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 282 |
{ok, http2} = gun:await_up(ConnPid), |
| 283 |
StreamRef = gun:get(ConnPid, "/stream_reply"), |
| 284 |
{error,{stream_error,{stream_error,internal_error,_}}} |
| 285 |
= gun:await(ConnPid, StreamRef), |
| 286 |
gun:close(ConnPid) |
| 287 |
after |
| 288 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 289 |
end. |
| 290 |
|
| 291 |
invalid_response_headers_stream_reply_ignore(Config) -> |
| 292 |
doc("Ensure invalid response headers are sent " |
| 293 |
"when allowed by configuration."), |
| 294 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 295 |
env => #{dispatch => init_dispatch(Config)}, |
| 296 |
invalid_response_headers => ignore |
| 297 |
}), |
| 298 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 299 |
try |
| 300 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 301 |
{ok, http2} = gun:await_up(ConnPid), |
| 302 |
StreamRef = gun:get(ConnPid, "/stream_reply"), |
| 303 |
%% Gun rejects invalid response headers with a protocol_error stream error. |
| 304 |
{error,{stream_error,{stream_error,protocol_error,_}}} |
| 305 |
= gun:await(ConnPid, StreamRef), |
| 306 |
gun:close(ConnPid) |
| 307 |
after |
| 308 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 309 |
end. |
| 310 |
|
| 311 |
invalid_response_headers_stream_trailers(Config) -> |
| 312 |
doc("Ensure invalid response headers are rejected by default."), |
| 313 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 314 |
env => #{dispatch => init_dispatch(Config)}, |
| 315 |
invalid_response_headers => error_terminate |
| 316 |
}), |
| 317 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 318 |
try |
| 319 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 320 |
{ok, http2} = gun:await_up(ConnPid), |
| 321 |
StreamRef = gun:get(ConnPid, "/stream_trailers", #{ |
| 322 |
<<"te">> => <<"trailers">> |
| 323 |
}), |
| 324 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef), |
| 325 |
{error,{stream_error,{stream_error,internal_error,_}}} |
| 326 |
= gun:await_body(ConnPid, StreamRef), |
| 327 |
gun:close(ConnPid) |
| 328 |
after |
| 329 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 330 |
end. |
| 331 |
|
| 332 |
invalid_response_headers_stream_trailers_ignore(Config) -> |
| 333 |
doc("Ensure invalid response headers are sent " |
| 334 |
"when allowed by configuration."), |
| 335 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 336 |
env => #{dispatch => init_dispatch(Config)}, |
| 337 |
invalid_response_headers => ignore |
| 338 |
}), |
| 339 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 340 |
try |
| 341 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 342 |
{ok, http2} = gun:await_up(ConnPid), |
| 343 |
StreamRef = gun:get(ConnPid, "/stream_trailers", #{ |
| 344 |
<<"te">> => <<"trailers">> |
| 345 |
}), |
| 346 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef), |
| 347 |
%% Gun rejects invalid response headers with a protocol_error stream error. |
| 348 |
{error,{stream_error,{stream_error,protocol_error,_}}} |
| 349 |
= gun:await_body(ConnPid, StreamRef), |
| 350 |
gun:close(ConnPid) |
| 351 |
after |
| 352 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 353 |
end. |
| 354 |
|
| 355 |
invalid_response_headers_switch_protocol(Config) -> |
| 356 |
doc("Ensure invalid response headers are rejected by default."), |
| 357 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 358 |
env => #{dispatch => init_dispatch(Config)} |
| 359 |
}), |
| 360 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 361 |
try |
| 362 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 363 |
{ok, http2} = gun:await_up(ConnPid), |
| 364 |
StreamRef = gun:get(ConnPid, "/switch_protocol"), |
| 365 |
{error,{stream_error,{stream_error,internal_error,_}}} |
| 366 |
= gun:await(ConnPid, StreamRef), |
| 367 |
gun:close(ConnPid) |
| 368 |
after |
| 369 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 370 |
end. |
| 371 |
|
| 372 |
invalid_response_headers_switch_protocol_ignore(Config) -> |
| 373 |
doc("Ensure invalid response headers are sent " |
| 374 |
"when allowed by configuration."), |
| 375 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 376 |
env => #{dispatch => init_dispatch(Config)}, |
| 377 |
invalid_response_headers => ignore |
| 378 |
}), |
| 379 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 380 |
try |
| 381 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 382 |
{ok, http2} = gun:await_up(ConnPid), |
| 383 |
StreamRef = gun:get(ConnPid, "/switch_protocol"), |
| 384 |
%% Gun rejects invalid response headers with a protocol_error stream error. |
| 385 |
{error,{stream_error,{stream_error,protocol_error,_}}} |
| 386 |
= gun:await(ConnPid, StreamRef), |
| 387 |
gun:close(ConnPid) |
| 388 |
after |
| 389 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 390 |
end. |
| 391 |
|
| 392 |
invalid_response_headers_push(Config) -> |
| 393 |
doc("Ensure invalid response headers in push are rejected by default."), |
| 394 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 395 |
env => #{dispatch => init_dispatch(Config)} |
| 396 |
}), |
| 397 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 398 |
try |
| 399 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 400 |
{ok, http2} = gun:await_up(ConnPid), |
| 401 |
StreamRef = gun:get(ConnPid, "/push"), |
| 402 |
{error,{stream_error,{stream_error,internal_error,_}}} |
| 403 |
= gun:await(ConnPid, StreamRef), |
| 404 |
gun:close(ConnPid) |
| 405 |
after |
| 406 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 407 |
end. |
| 408 |
|
| 409 |
invalid_response_headers_push_ignore(Config) -> |
| 410 |
doc("Ensure invalid response headers in push are sent " |
| 411 |
"when allowed by configuration."), |
| 412 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 413 |
env => #{dispatch => init_dispatch(Config)}, |
| 414 |
invalid_response_headers => ignore |
| 415 |
}), |
| 416 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 417 |
try |
| 418 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 419 |
{ok, http2} = gun:await_up(ConnPid), |
| 420 |
StreamRef = gun:get(ConnPid, "/push"), |
| 421 |
%% The pushed response doesn't go through because |
| 422 |
%% Gun rejects it as a protocol error. |
| 423 |
{response, _, 200, _} = gun:await(ConnPid, StreamRef), |
| 424 |
gun:close(ConnPid) |
| 425 |
after |
| 426 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 427 |
end. |
| 428 |
|
| 429 |
invalid_response_headers_early_error(Config) -> |
| 430 |
doc("Ensure invalid response headers are rejected by default."), |
| 431 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 432 |
env => #{dispatch => init_dispatch(Config)}, |
| 433 |
stream_handlers => [stream_handler_invalid_headers_h] |
| 434 |
}), |
| 435 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 436 |
try |
| 437 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 438 |
{ok, http2} = gun:await_up(ConnPid), |
| 439 |
StreamRef = gun:headers(ConnPid, "TRACE", "/", []), |
| 440 |
{error,{stream_error,{stream_error,internal_error,_}}} |
| 441 |
= gun:await(ConnPid, StreamRef), |
| 442 |
gun:close(ConnPid) |
| 443 |
after |
| 444 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 445 |
end. |
| 446 |
|
| 447 |
invalid_response_headers_early_error_ignore(Config) -> |
| 448 |
doc("Ensure invalid response headers are sent " |
| 449 |
"when allowed by configuration."), |
| 450 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 451 |
env => #{dispatch => init_dispatch(Config)}, |
| 452 |
invalid_response_headers => ignore, |
| 453 |
stream_handlers => [stream_handler_invalid_headers_h] |
| 454 |
}), |
| 455 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 456 |
try |
| 457 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 458 |
{ok, http2} = gun:await_up(ConnPid), |
| 459 |
StreamRef = gun:headers(ConnPid, "TRACE", "/", []), |
| 460 |
%% Gun rejects invalid response headers with a protocol_error stream error. |
| 461 |
{error,{stream_error,{stream_error,protocol_error,_}}} |
| 462 |
= gun:await(ConnPid, StreamRef), |
| 463 |
gun:close(ConnPid) |
| 464 |
after |
| 465 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 466 |
end. |
| 467 |
|
| 468 |
max_authority_length_authority_pseudo_header(Config0) -> |
| 469 |
doc("Confirm the max_authority_length option correctly limits " |
| 470 |
"the length of the authority component in the :authority pseudo-header."), |
| 471 |
ProtoOpts = #{ |
| 472 |
env => #{dispatch => init_dispatch(Config0)}, |
| 473 |
max_authority_length => 10 |
| 474 |
}, |
| 475 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 476 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 477 |
Config = [{port, Port}|Config0], |
| 478 |
try |
| 479 |
%% At limit. |
| 480 |
{ok, Socket1} = do_handshake(Config), |
| 481 |
Headers1 = [ |
| 482 |
{<<":method">>, <<"GET">>}, |
| 483 |
{<<":scheme">>, <<"http">>}, |
| 484 |
{<<":authority">>, <<"example.co">>}, |
| 485 |
{<<":path">>, <<"/">>} |
| 486 |
], |
| 487 |
{HeadersBlock1, _} = cow_hpack:encode(Headers1), |
| 488 |
ok = gen_tcp:send(Socket1, cow_http2:headers(1, fin, HeadersBlock1)), |
| 489 |
{ok, <<_:24, 1:8, _:40>>} = gen_tcp:recv(Socket1, 9, 6000), |
| 490 |
gen_tcp:close(Socket1), |
| 491 |
%% Over limit. |
| 492 |
{ok, Socket2} = do_handshake(Config), |
| 493 |
Headers2 = [ |
| 494 |
{<<":method">>, <<"GET">>}, |
| 495 |
{<<":scheme">>, <<"http">>}, |
| 496 |
{<<":authority">>, <<"example.com">>}, |
| 497 |
{<<":path">>, <<"/">>} |
| 498 |
], |
| 499 |
{HeadersBlock2, _} = cow_hpack:encode(Headers2), |
| 500 |
ok = gen_tcp:send(Socket2, cow_http2:headers(1, fin, HeadersBlock2)), |
| 501 |
{ok, <<_:24, 3:8, _:8, 1:32, 1:32>>} = gen_tcp:recv(Socket2, 13, 6000), |
| 502 |
gen_tcp:close(Socket2) |
| 503 |
after |
| 504 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 505 |
end. |
| 506 |
|
| 507 |
max_authority_length_host_header(Config0) -> |
| 508 |
doc("Confirm the max_authority_length option correctly limits " |
| 509 |
"the length of the authority component in the host header."), |
| 510 |
ProtoOpts = #{ |
| 511 |
env => #{dispatch => init_dispatch(Config0)}, |
| 512 |
max_authority_length => 10 |
| 513 |
}, |
| 514 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 515 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 516 |
Config = [{port, Port}|Config0], |
| 517 |
try |
| 518 |
%% At limit. |
| 519 |
{ok, Socket1} = do_handshake(Config), |
| 520 |
Headers1 = [ |
| 521 |
{<<":method">>, <<"GET">>}, |
| 522 |
{<<":scheme">>, <<"http">>}, |
| 523 |
{<<":path">>, <<"/">>}, |
| 524 |
{<<"host">>, <<"example.co">>} |
| 525 |
], |
| 526 |
{HeadersBlock1, _} = cow_hpack:encode(Headers1), |
| 527 |
ok = gen_tcp:send(Socket1, cow_http2:headers(1, fin, HeadersBlock1)), |
| 528 |
{ok, <<_:24, 1:8, _:40>>} = gen_tcp:recv(Socket1, 9, 6000), |
| 529 |
gen_tcp:close(Socket1), |
| 530 |
%% Over limit. |
| 531 |
{ok, Socket2} = do_handshake(Config), |
| 532 |
Headers2 = [ |
| 533 |
{<<":method">>, <<"GET">>}, |
| 534 |
{<<":scheme">>, <<"http">>}, |
| 535 |
{<<":path">>, <<"/">>}, |
| 536 |
{<<"host">>, <<"example.com">>} |
| 537 |
], |
| 538 |
{HeadersBlock2, _} = cow_hpack:encode(Headers2), |
| 539 |
ok = gen_tcp:send(Socket2, cow_http2:headers(1, fin, HeadersBlock2)), |
| 540 |
{ok, <<_:24, 3:8, _:8, 1:32, 1:32>>} = gen_tcp:recv(Socket2, 13, 6000), |
| 541 |
gen_tcp:close(Socket2) |
| 542 |
after |
| 543 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 544 |
end. |
| 545 |
|
| 546 |
max_frame_size_sent(Config) -> |
| 547 |
doc("Confirm that frames sent by Cowboy are limited in size " |
| 548 |
"by the max_frame_size_sent configuration value."), |
| 549 |
MaxFrameSize = 20000, |
| 550 |
ProtoOpts = #{ |
| 551 |
env => #{dispatch => init_dispatch(Config)}, |
| 552 |
max_frame_size_sent => MaxFrameSize |
| 553 |
}, |
| 554 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 555 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 556 |
try |
| 557 |
{ok, Socket} = do_handshake(#{max_frame_size => MaxFrameSize + 10000}, |
| 558 |
[{port, Port}|Config]), |
| 559 |
%% Send a request with a 30000 bytes body. |
| 560 |
{HeadersBlock, _} = cow_hpack:encode([ |
| 561 |
{<<":method">>, <<"POST">>}, |
| 562 |
{<<":scheme">>, <<"http">>}, |
| 563 |
{<<":authority">>, <<"localhost">>}, %% @todo Correct port number. |
| 564 |
{<<":path">>, <<"/echo/read_body">>} |
| 565 |
]), |
| 566 |
ok = gen_tcp:send(Socket, [ |
| 567 |
cow_http2:headers(1, nofin, HeadersBlock), |
| 568 |
cow_http2:data(1, nofin, <<0:16384/unit:8>>), |
| 569 |
cow_http2:data(1, fin, <<0:13616/unit:8>>) |
| 570 |
]), |
| 571 |
%% Receive a HEADERS frame as a response. |
| 572 |
{ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = case gen_tcp:recv(Socket, 9, 1000) of |
| 573 |
%% We received a WINDOW_UPDATE first. Skip it and the next. |
| 574 |
{ok, <<4:24, 8:8, 0:40>>} -> |
| 575 |
{ok, _} = gen_tcp:recv(Socket, 4 + 13, 1000), |
| 576 |
gen_tcp:recv(Socket, 9, 1000); |
| 577 |
Res -> |
| 578 |
Res |
| 579 |
end, |
| 580 |
{ok, _} = gen_tcp:recv(Socket, SkipLen, 6000), |
| 581 |
%% The DATA frames following must have lengths of 20000 |
| 582 |
%% and then 10000 due to the limit. |
| 583 |
{ok, <<20000:24, 0:8, _:40, _:20000/unit:8>>} = gen_tcp:recv(Socket, 20009, 6000), |
| 584 |
{ok, <<10000:24, 0:8, _:40, _:10000/unit:8>>} = gen_tcp:recv(Socket, 10009, 6000), |
| 585 |
gen_tcp:close(Socket) |
| 586 |
after |
| 587 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 588 |
end. |
| 589 |
|
| 590 |
max_headers(Config0) -> |
| 591 |
doc("Confirm that headers received by Cowboy are limited in number " |
| 592 |
"by the max_headers configuration value."), |
| 593 |
ProtoOpts = #{ |
| 594 |
env => #{dispatch => init_dispatch(Config0)}, |
| 595 |
max_headers => 10000 |
| 596 |
}, |
| 597 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 598 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 599 |
Config = [{port, Port}|Config0], |
| 600 |
try |
| 601 |
%% Sending exactly 10000 headers. |
| 602 |
{ok, Socket1} = do_handshake(Config), |
| 603 |
Headers1 = [ |
| 604 |
{<<":method">>, <<"GET">>}, |
| 605 |
{<<":scheme">>, <<"http">>}, |
| 606 |
{<<":authority">>, <<"localhost">>}, |
| 607 |
{<<":path">>, <<"/">>} |
| 608 |
|lists:duplicate(9996, {<<"x-custom">>, <<"value">>}) |
| 609 |
], |
| 610 |
{HeadersBlock1, _} = cow_hpack:encode(Headers1), |
| 611 |
ok = gen_tcp:send(Socket1, cow_http2:headers(1, fin, HeadersBlock1)), |
| 612 |
%% Success: we get a response back. |
| 613 |
{ok, <<_:24, 1:8, _:40>>} = gen_tcp:recv(Socket1, 9, 6000), |
| 614 |
gen_tcp:close(Socket1), |
| 615 |
%% Sending 10001 headers, above the limit. |
| 616 |
{ok, Socket2} = do_handshake(Config), |
| 617 |
Headers2 = [ |
| 618 |
{<<":method">>, <<"GET">>}, |
| 619 |
{<<":scheme">>, <<"http">>}, |
| 620 |
{<<":authority">>, <<"localhost">>}, |
| 621 |
{<<":path">>, <<"/">>} |
| 622 |
|lists:duplicate(9997, {<<"x-custom">>, <<"value">>}) |
| 623 |
], |
| 624 |
{HeadersBlock2, _} = cow_hpack:encode(Headers2), |
| 625 |
ok = gen_tcp:send(Socket2, cow_http2:headers(1, fin, HeadersBlock2)), |
| 626 |
%% Receive an ENHANCE_YOUR_CALM connection error. |
| 627 |
{ok, <<_:24, 7:8, _:72, 11:32>>} = gen_tcp:recv(Socket2, 17, 6000), |
| 628 |
ok |
| 629 |
after |
| 630 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 631 |
end. |
| 632 |
|
| 633 |
persistent_term_router(Config) -> |
| 634 |
doc("The router can retrieve the routes from persistent_term storage."), |
| 635 |
case erlang:function_exported(persistent_term, get, 1) of |
| 636 |
true -> do_persistent_term_router(Config); |
| 637 |
false -> {skip, "This test uses the persistent_term functionality added in Erlang/OTP 21.2."} |
| 638 |
end. |
| 639 |
|
| 640 |
do_persistent_term_router(Config) -> |
| 641 |
persistent_term:put(?FUNCTION_NAME, init_dispatch(Config)), |
| 642 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 643 |
env => #{dispatch => {persistent_term, ?FUNCTION_NAME}} |
| 644 |
}), |
| 645 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 646 |
try |
| 647 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 648 |
{ok, http2} = gun:await_up(ConnPid), |
| 649 |
StreamRef = gun:get(ConnPid, "/"), |
| 650 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef), |
| 651 |
gun:close(ConnPid) |
| 652 |
after |
| 653 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 654 |
end. |
| 655 |
|
| 656 |
preface_timeout_infinity(Config) -> |
| 657 |
doc("Ensure infinity for preface_timeout is accepted."), |
| 658 |
ProtoOpts = #{ |
| 659 |
env => #{dispatch => init_dispatch(Config)}, |
| 660 |
preface_timeout => infinity |
| 661 |
}, |
| 662 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 663 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 664 |
try |
| 665 |
{ok, Socket} = do_handshake([{port, Port}|Config]), |
| 666 |
Pid = get_remote_pid_tcp(Socket), |
| 667 |
Ref = erlang:monitor(process, Pid), |
| 668 |
receive |
| 669 |
{'DOWN', Ref, process, Pid, Reason} -> |
| 670 |
error(Reason) |
| 671 |
after 1000 -> |
| 672 |
gen_tcp:close(Socket) |
| 673 |
end |
| 674 |
after |
| 675 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 676 |
end. |
| 677 |
|
| 678 |
resp_iolist_body(Config) -> |
| 679 |
doc("Regression test when response bodies are iolists that " |
| 680 |
"include improper lists, empty lists and empty binaries. " |
| 681 |
"The original issue failed to split the body into frames properly."), |
| 682 |
ProtoOpts = #{ |
| 683 |
env => #{dispatch => init_dispatch(Config)} |
| 684 |
}, |
| 685 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 686 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 687 |
try |
| 688 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 689 |
Ref = gun:get(ConnPid, "/resp_iolist_body"), |
| 690 |
{response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref), |
| 691 |
{_, BinLen} = lists:keyfind(<<"content-length">>, 1, RespHeaders), |
| 692 |
Len = binary_to_integer(BinLen), |
| 693 |
{ok, RespBody} = gun:await_body(ConnPid, Ref), |
| 694 |
Len = iolist_size(RespBody), |
| 695 |
gun:close(ConnPid) |
| 696 |
after |
| 697 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 698 |
end. |
| 699 |
|
| 700 |
settings_timeout_infinity(Config) -> |
| 701 |
doc("Ensure infinity for settings_timeout is accepted."), |
| 702 |
ProtoOpts = #{ |
| 703 |
env => #{dispatch => init_dispatch(Config)}, |
| 704 |
settings_timeout => infinity |
| 705 |
}, |
| 706 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 707 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 708 |
try |
| 709 |
{ok, Socket} = do_handshake([{port, Port}|Config]), |
| 710 |
Pid = get_remote_pid_tcp(Socket), |
| 711 |
Ref = erlang:monitor(process, Pid), |
| 712 |
receive |
| 713 |
{'DOWN', Ref, process, Pid, Reason} -> |
| 714 |
error(Reason) |
| 715 |
after 1000 -> |
| 716 |
gen_tcp:close(Socket) |
| 717 |
end |
| 718 |
after |
| 719 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 720 |
end. |
| 721 |
|
| 722 |
graceful_shutdown_connection(Config) -> |
| 723 |
doc("Check that ongoing requests are handled before gracefully shutting down a connection."), |
| 724 |
Dispatch = cowboy_router:compile([{"localhost", [ |
| 725 |
{"/delay_hello", delay_hello_h, |
| 726 |
#{delay => 500, notify_received => self()}} |
| 727 |
]}]), |
| 728 |
ProtoOpts = #{ |
| 729 |
env => #{dispatch => Dispatch} |
| 730 |
}, |
| 731 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 732 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 733 |
try |
| 734 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 735 |
Ref = gun:get(ConnPid, "/delay_hello"), |
| 736 |
%% Make sure the request is received. |
| 737 |
receive {request_received, <<"/delay_hello">>} -> ok end, |
| 738 |
%% Tell the connection to shutdown while the handler is working. |
| 739 |
[CowboyConnPid] = ranch:procs(?FUNCTION_NAME, connections), |
| 740 |
monitor(process, CowboyConnPid), |
| 741 |
ok = sys:terminate(CowboyConnPid, goaway), |
| 742 |
%% Check that the response is sent to the client before the |
| 743 |
%% connection goes down. |
| 744 |
{response, nofin, 200, _RespHeaders} = gun:await(ConnPid, Ref), |
| 745 |
{ok, RespBody} = gun:await_body(ConnPid, Ref), |
| 746 |
<<"Hello world!">> = iolist_to_binary(RespBody), |
| 747 |
%% Check that the connection is gone soon afterwards. (The exit |
| 748 |
%% reason is supposed to be 'goaway' as passed to |
| 749 |
%% sys:terminate/2, but it is {shutdown, closed}.) |
| 750 |
receive |
| 751 |
{'DOWN', _, process, CowboyConnPid, _Reason} -> |
| 752 |
ok |
| 753 |
end, |
| 754 |
[] = ranch:procs(?FUNCTION_NAME, connections), |
| 755 |
gun:close(ConnPid) |
| 756 |
after |
| 757 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 758 |
end. |
| 759 |
|
| 760 |
graceful_shutdown_timeout(Config) -> |
| 761 |
doc("Check that a connection is closed when gracefully shutting down times out."), |
| 762 |
Dispatch = cowboy_router:compile([{"localhost", [ |
| 763 |
{"/long_delay_hello", delay_hello_h, |
| 764 |
#{delay => 10000, notify_received => self()}} |
| 765 |
]}]), |
| 766 |
ProtoOpts = #{ |
| 767 |
env => #{dispatch => Dispatch}, |
| 768 |
goaway_initial_timeout => 200, |
| 769 |
goaway_complete_timeout => 500 |
| 770 |
}, |
| 771 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 772 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 773 |
try |
| 774 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 775 |
Ref = gun:get(ConnPid, "/long_delay_hello"), |
| 776 |
%% Make sure the request is received. |
| 777 |
receive {request_received, <<"/long_delay_hello">>} -> ok end, |
| 778 |
%% Tell the connection to shutdown while the handler is working. |
| 779 |
[CowboyConnPid] = ranch:procs(?FUNCTION_NAME, connections), |
| 780 |
monitor(process, CowboyConnPid), |
| 781 |
ok = sys:terminate(CowboyConnPid, goaway), |
| 782 |
%% Check that connection didn't wait for the slow handler. |
| 783 |
{error, {stream_error, closed}} = gun:await(ConnPid, Ref), |
| 784 |
%% Check that the connection is gone. (The exit reason is |
| 785 |
%% supposed to be 'goaway' as passed to sys:terminate/2, but it |
| 786 |
%% is {shutdown, {stop, {exit, goaway}, 'Graceful shutdown timed |
| 787 |
%% out.'}}.) |
| 788 |
receive |
| 789 |
{'DOWN', _, process, CowboyConnPid, _Reason} -> |
| 790 |
ok |
| 791 |
after 100 -> |
| 792 |
error(still_alive) |
| 793 |
end, |
| 794 |
[] = ranch:procs(?FUNCTION_NAME, connections), |
| 795 |
gun:close(ConnPid) |
| 796 |
after |
| 797 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 798 |
end. |
| 799 |
|
| 800 |
graceful_shutdown_listener(Config) -> |
| 801 |
doc("Check that connections are shut down gracefully when stopping a listener."), |
| 802 |
TransOpts = #{ |
| 803 |
socket_opts => [{port, 0}], |
| 804 |
shutdown => 1000 %% Shorter timeout to make the test case faster. |
| 805 |
}, |
| 806 |
Dispatch = cowboy_router:compile([{"localhost", [ |
| 807 |
{"/delay_hello", delay_hello_h, |
| 808 |
#{delay => 500, notify_received => self()}} |
| 809 |
]}]), |
| 810 |
ProtoOpts = #{ |
| 811 |
env => #{dispatch => Dispatch} |
| 812 |
}, |
| 813 |
{ok, Listener} = cowboy:start_clear(?FUNCTION_NAME, TransOpts, ProtoOpts), |
| 814 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 815 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 816 |
Ref = gun:get(ConnPid, "/delay_hello"), |
| 817 |
%% Shutdown listener while the handlers are working. |
| 818 |
receive {request_received, <<"/delay_hello">>} -> ok end, |
| 819 |
ListenerMonitorRef = monitor(process, Listener), |
| 820 |
%% Note: This call does not complete quickly and will |
| 821 |
%% prevent other cowboy:stop_listener/1 calls to complete. |
| 822 |
ok = cowboy:stop_listener(?FUNCTION_NAME), |
| 823 |
receive |
| 824 |
{'DOWN', ListenerMonitorRef, process, Listener, _Reason} -> |
| 825 |
ok |
| 826 |
end, |
| 827 |
%% Check that the request is handled before shutting down. |
| 828 |
{response, nofin, 200, _RespHeaders} = gun:await(ConnPid, Ref), |
| 829 |
{ok, RespBody} = gun:await_body(ConnPid, Ref), |
| 830 |
<<"Hello world!">> = iolist_to_binary(RespBody), |
| 831 |
gun:close(ConnPid). |
| 832 |
|
| 833 |
graceful_shutdown_listener_timeout(Config) -> |
| 834 |
doc("Check that connections are shut down when gracefully stopping a listener times out."), |
| 835 |
TransOpts = #{ |
| 836 |
socket_opts => [{port, 0}], |
| 837 |
shutdown => 1000 %% Shorter timeout to make the test case faster. |
| 838 |
}, |
| 839 |
Dispatch = cowboy_router:compile([{"localhost", [ |
| 840 |
{"/long_delay_hello", delay_hello_h, |
| 841 |
#{delay => 10000, notify_received => self()}} |
| 842 |
]}]), |
| 843 |
ProtoOpts = #{ |
| 844 |
env => #{dispatch => Dispatch}, |
| 845 |
goaway_initial_timeout => 200, |
| 846 |
goaway_complete_timeout => 500 |
| 847 |
}, |
| 848 |
{ok, Listener} = cowboy:start_clear(?FUNCTION_NAME, TransOpts, ProtoOpts), |
| 849 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 850 |
ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]), |
| 851 |
Ref = gun:get(ConnPid, "/long_delay_hello"), |
| 852 |
%% Shutdown listener while the handlers are working. |
| 853 |
receive {request_received, <<"/long_delay_hello">>} -> ok end, |
| 854 |
ListenerMonitorRef = monitor(process, Listener), |
| 855 |
%% Note: This call does not complete quickly and will |
| 856 |
%% prevent other cowboy:stop_listener/1 calls to complete. |
| 857 |
ok = cowboy:stop_listener(?FUNCTION_NAME), |
| 858 |
receive |
| 859 |
{'DOWN', ListenerMonitorRef, process, Listener, _Reason} -> |
| 860 |
ok |
| 861 |
end, |
| 862 |
%% Check that the slow request is aborted. |
| 863 |
{error, {stream_error, closed}} = gun:await(ConnPid, Ref), |
| 864 |
gun:close(ConnPid). |
| 865 |
|
| 866 |
send_timeout_close(Config) -> |
| 867 |
doc("Check that connections are closed on send timeout."), |
| 868 |
TransOpts = #{ |
| 869 |
socket_opts => [ |
| 870 |
{port, 0}, |
| 871 |
{send_timeout, 100}, |
| 872 |
{send_timeout_close, true}, |
| 873 |
{sndbuf, 10} |
| 874 |
] |
| 875 |
}, |
| 876 |
Dispatch = cowboy_router:compile([{"localhost", [ |
| 877 |
{"/endless", loop_handler_endless_h, #{delay => 100}} |
| 878 |
]}]), |
| 879 |
ProtoOpts = #{ |
| 880 |
env => #{dispatch => Dispatch}, |
| 881 |
idle_timeout => infinity |
| 882 |
}, |
| 883 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, TransOpts, ProtoOpts), |
| 884 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 885 |
try |
| 886 |
%% Connect a client that sends a request and waits indefinitely. |
| 887 |
{ok, ClientSocket} = do_handshake([{port, Port}, |
| 888 |
{tcp_opts, [{recbuf, 10}, {buffer, 10}, {active, false}]}|Config]), |
| 889 |
{HeadersBlock, _} = cow_hpack:encode([ |
| 890 |
{<<":method">>, <<"GET">>}, |
| 891 |
{<<":scheme">>, <<"http">>}, |
| 892 |
{<<":authority">>, <<"localhost">>}, %% @todo Correct port number. |
| 893 |
{<<":path">>, <<"/endless">>}, |
| 894 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 895 |
]), |
| 896 |
ok = gen_tcp:send(ClientSocket, [ |
| 897 |
cow_http2:headers(1, fin, HeadersBlock), |
| 898 |
%% Greatly increase the window to make sure we don't run |
| 899 |
%% out of space before we get send timeouts. |
| 900 |
cow_http2:window_update(10000000), |
| 901 |
cow_http2:window_update(1, 10000000) |
| 902 |
]), |
| 903 |
%% Wait for the handler to start then get its pid, |
| 904 |
%% the remote connection's pid and socket. |
| 905 |
StreamPid = receive |
| 906 |
{Self, StreamPid0, init} when Self =:= self() -> |
| 907 |
StreamPid0 |
| 908 |
after 1000 -> |
| 909 |
error(timeout) |
| 910 |
end, |
| 911 |
ServerPid = ct_helper:get_remote_pid_tcp(ClientSocket), |
| 912 |
{links, ServerLinks} = process_info(ServerPid, links), |
| 913 |
[ServerSocket] = [PidOrPort || PidOrPort <- ServerLinks, is_port(PidOrPort)], |
| 914 |
%% Poll the socket repeatedly until it is closed by the server. |
| 915 |
WaitClosedFun = |
| 916 |
fun F(T) when T =< 0 -> |
| 917 |
error({status, prim_inet:getstatus(ServerSocket)}); |
| 918 |
F(T) -> |
| 919 |
Snooze = 100, |
| 920 |
case inet:sockname(ServerSocket) of |
| 921 |
{error, _} -> |
| 922 |
timer:sleep(Snooze); |
| 923 |
{ok, _} -> |
| 924 |
timer:sleep(Snooze), |
| 925 |
F(T - Snooze) |
| 926 |
end |
| 927 |
end, |
| 928 |
ok = WaitClosedFun(2000), |
| 929 |
false = erlang:is_process_alive(StreamPid), |
| 930 |
false = erlang:is_process_alive(ServerPid), |
| 931 |
gen_tcp:close(ClientSocket) |
| 932 |
after |
| 933 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 934 |
end. |