| 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(http_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 |
-import(cowboy_test, [gun_down/1]). |
| 24 |
-import(cowboy_test, [raw_open/1]). |
| 25 |
-import(cowboy_test, [raw_send/2]). |
| 26 |
-import(cowboy_test, [raw_recv_head/1]). |
| 27 |
-import(cowboy_test, [raw_recv_rest/3]). |
| 28 |
-import(cowboy_test, [raw_recv/3]). |
| 29 |
-import(cowboy_test, [raw_expect_recv/2]). |
| 30 |
|
| 31 |
-include_lib("stdlib/include/assert.hrl"). |
| 32 |
|
| 33 |
all() -> |
| 34 |
[{group, clear_no_parallel}, {group, clear}]. |
| 35 |
|
| 36 |
groups() -> |
| 37 |
[ |
| 38 |
%% cowboy:stop_listener can be slow when called many times |
| 39 |
%% in parallel so we must run this test separately from the others. |
| 40 |
{clear_no_parallel, [], [graceful_shutdown_listener]}, |
| 41 |
{clear, [parallel], ct_helper:all(?MODULE) -- [graceful_shutdown_listener]} |
| 42 |
]. |
| 43 |
|
| 44 |
init_per_group(Name, Config) -> |
| 45 |
cowboy_test:init_http(Name, #{ |
| 46 |
env => #{dispatch => init_dispatch(Config)} |
| 47 |
}, Config). |
| 48 |
|
| 49 |
end_per_group(Name, _) -> |
| 50 |
cowboy:stop_listener(Name). |
| 51 |
|
| 52 |
init_dispatch(_) -> |
| 53 |
cowboy_router:compile([{"localhost", [ |
| 54 |
{"/", hello_h, []}, |
| 55 |
{"/delay_hello", delay_hello_h, #{delay => 1000, notify_received => self()}}, |
| 56 |
{"/echo/:key", echo_h, []}, |
| 57 |
{"/resp/:key[/:arg]", resp_h, []}, |
| 58 |
{"/set_options/:key", set_options_h, []}, |
| 59 |
{"/streamed_result/:n/:interval", streamed_result_h, []} |
| 60 |
]}]). |
| 61 |
|
| 62 |
chunked_false(Config) -> |
| 63 |
doc("Confirm the option chunked => false disables chunked " |
| 64 |
"transfer-encoding for HTTP/1.1 connections."), |
| 65 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 66 |
env => #{dispatch => init_dispatch(Config)}, |
| 67 |
chunked => false |
| 68 |
}), |
| 69 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 70 |
try |
| 71 |
Request = "GET /resp/stream_reply2/200 HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 72 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 73 |
ok = raw_send(Client, Request), |
| 74 |
Data = raw_recv_head(Client), |
| 75 |
%% Cowboy always advertises itself as HTTP/1.1. |
| 76 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data), |
| 77 |
{Headers, Rest} = cow_http:parse_headers(Rest0), |
| 78 |
false = lists:keyfind(<<"content-length">>, 1, Headers), |
| 79 |
false = lists:keyfind(<<"transfer-encoding">>, 1, Headers), |
| 80 |
Bits = 8000000 - bit_size(Rest), |
| 81 |
raw_expect_recv(Client, <<0:Bits>>), |
| 82 |
{error, closed} = raw_recv(Client, 1, 1000) |
| 83 |
after |
| 84 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 85 |
end. |
| 86 |
|
| 87 |
chunked_one_byte_at_a_time(Config) -> |
| 88 |
doc("Confirm that chunked transfer-encoding works when " |
| 89 |
"the body is received one byte at a time."), |
| 90 |
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), |
| 91 |
ChunkedBody = iolist_to_binary(do_chunked_body(50, Body, [])), |
| 92 |
Client = raw_open(Config), |
| 93 |
ok = raw_send(Client, |
| 94 |
"POST /echo/read_body HTTP/1.1\r\n" |
| 95 |
"Host: localhost\r\n" |
| 96 |
"Transfer-encoding: chunked\r\n\r\n"), |
| 97 |
_ = [begin |
| 98 |
raw_send(Client, <<C>>), |
| 99 |
timer:sleep(1) |
| 100 |
end || <<C>> <= ChunkedBody], |
| 101 |
Data = raw_recv_head(Client), |
| 102 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data), |
| 103 |
{_, Rest} = cow_http:parse_headers(Rest0), |
| 104 |
RestSize = byte_size(Rest), |
| 105 |
<<Rest:RestSize/binary, Expect/bits>> = Body, |
| 106 |
raw_expect_recv(Client, Expect). |
| 107 |
|
| 108 |
chunked_one_chunk_at_a_time(Config) -> |
| 109 |
doc("Confirm that chunked transfer-encoding works when " |
| 110 |
"the body is received one chunk at a time."), |
| 111 |
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), |
| 112 |
Chunks = do_chunked_body(50, Body, []), |
| 113 |
Client = raw_open(Config), |
| 114 |
ok = raw_send(Client, |
| 115 |
"POST /echo/read_body HTTP/1.1\r\n" |
| 116 |
"Host: localhost\r\n" |
| 117 |
"Transfer-encoding: chunked\r\n\r\n"), |
| 118 |
_ = [begin |
| 119 |
raw_send(Client, Chunk), |
| 120 |
timer:sleep(10) |
| 121 |
end || Chunk <- Chunks], |
| 122 |
Data = raw_recv_head(Client), |
| 123 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data), |
| 124 |
{_, Rest} = cow_http:parse_headers(Rest0), |
| 125 |
RestSize = byte_size(Rest), |
| 126 |
<<Rest:RestSize/binary, Expect/bits>> = Body, |
| 127 |
raw_expect_recv(Client, Expect). |
| 128 |
|
| 129 |
chunked_split_delay_in_chunk_body(Config) -> |
| 130 |
doc("Confirm that chunked transfer-encoding works when " |
| 131 |
"the body is received with a delay inside the chunks."), |
| 132 |
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), |
| 133 |
Chunks = do_chunked_body(50, Body, []), |
| 134 |
Client = raw_open(Config), |
| 135 |
ok = raw_send(Client, |
| 136 |
"POST /echo/read_body HTTP/1.1\r\n" |
| 137 |
"Host: localhost\r\n" |
| 138 |
"Transfer-encoding: chunked\r\n\r\n"), |
| 139 |
_ = [begin |
| 140 |
case Chunk of |
| 141 |
<<"0\r\n\r\n">> -> |
| 142 |
raw_send(Client, Chunk); |
| 143 |
_ -> |
| 144 |
[Size, ChunkBody, <<>>] = binary:split(Chunk, <<"\r\n">>, [global]), |
| 145 |
PartASize = rand:uniform(byte_size(ChunkBody)), |
| 146 |
<<PartA:PartASize/binary, PartB/binary>> = ChunkBody, |
| 147 |
raw_send(Client, [Size, <<"\r\n">>, PartA]), |
| 148 |
timer:sleep(10), |
| 149 |
raw_send(Client, [PartB, <<"\r\n">>]) |
| 150 |
end |
| 151 |
end || Chunk <- Chunks], |
| 152 |
Data = raw_recv_head(Client), |
| 153 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data), |
| 154 |
{_, Rest} = cow_http:parse_headers(Rest0), |
| 155 |
RestSize = byte_size(Rest), |
| 156 |
<<Rest:RestSize/binary, Expect/bits>> = Body, |
| 157 |
raw_expect_recv(Client, Expect). |
| 158 |
|
| 159 |
chunked_split_delay_in_chunk_crlf(Config) -> |
| 160 |
doc("Confirm that chunked transfer-encoding works when " |
| 161 |
"the body is received with a delay inside the chunks end CRLF."), |
| 162 |
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), |
| 163 |
Chunks = do_chunked_body(50, Body, []), |
| 164 |
Client = raw_open(Config), |
| 165 |
ok = raw_send(Client, |
| 166 |
"POST /echo/read_body HTTP/1.1\r\n" |
| 167 |
"Host: localhost\r\n" |
| 168 |
"Transfer-encoding: chunked\r\n\r\n"), |
| 169 |
_ = [begin |
| 170 |
Len = byte_size(Chunk) - (rand:uniform(2) - 1), |
| 171 |
<<Begin:Len/binary, End/binary>> = Chunk, |
| 172 |
raw_send(Client, Begin), |
| 173 |
timer:sleep(10), |
| 174 |
raw_send(Client, End) |
| 175 |
end || Chunk <- Chunks], |
| 176 |
Data = raw_recv_head(Client), |
| 177 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data), |
| 178 |
{_, Rest} = cow_http:parse_headers(Rest0), |
| 179 |
RestSize = byte_size(Rest), |
| 180 |
<<Rest:RestSize/binary, Expect/bits>> = Body, |
| 181 |
raw_expect_recv(Client, Expect). |
| 182 |
|
| 183 |
do_chunked_body(_, <<>>, Acc) -> |
| 184 |
lists:reverse([cow_http_te:last_chunk()|Acc]); |
| 185 |
do_chunked_body(ChunkSize0, Data, Acc) -> |
| 186 |
ChunkSize = min(byte_size(Data), ChunkSize0), |
| 187 |
<<Chunk:ChunkSize/binary, Rest/binary>> = Data, |
| 188 |
do_chunked_body(ChunkSize, Rest, |
| 189 |
[iolist_to_binary(cow_http_te:chunk(Chunk))|Acc]). |
| 190 |
|
| 191 |
chunked_trailers_skip(Config) -> |
| 192 |
doc("Confirm that request trailers are skipped and another " |
| 193 |
"request can be sent on the same connection."), |
| 194 |
Client = raw_open(Config), |
| 195 |
ok = raw_send(Client, |
| 196 |
"POST /echo/read_body HTTP/1.1\r\n" |
| 197 |
"Host: localhost\r\n" |
| 198 |
"Transfer-Encoding: chunked\r\n" |
| 199 |
"Trailer: x-foo\r\n" |
| 200 |
"\r\n" |
| 201 |
"5\r\nHello\r\n" |
| 202 |
"0\r\n" |
| 203 |
"X-Foo: bar\r\n" |
| 204 |
"\r\n"), |
| 205 |
Data = raw_recv_head(Client), |
| 206 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data), |
| 207 |
{_, Rest} = cow_http:parse_headers(Rest0), |
| 208 |
RestSize = byte_size(Rest), |
| 209 |
<<Rest:RestSize/binary, Expect/bits>> = <<"Hello">>, |
| 210 |
raw_expect_recv(Client, Expect), |
| 211 |
%% Second request/response on the same connection. |
| 212 |
ok = raw_send(Client, |
| 213 |
"GET / HTTP/1.1\r\n" |
| 214 |
"Host: localhost\r\n" |
| 215 |
"\r\n"), |
| 216 |
Data2 = raw_recv_head(Client), |
| 217 |
{'HTTP/1.1', 200, _, _} = cow_http:parse_status_line(Data2), |
| 218 |
ok. |
| 219 |
|
| 220 |
chunked_trailers_skip_limit(Config) -> |
| 221 |
doc("Confirm that the trailer size limit is enforced and " |
| 222 |
"results in a 400 error and the closing of the connection."), |
| 223 |
Client = raw_open(Config), |
| 224 |
Large = binary:copy(<<"a">>, 5000), |
| 225 |
ok = raw_send(Client, [ |
| 226 |
"POST /delay_hello HTTP/1.1\r\n" |
| 227 |
"Host: localhost\r\n" |
| 228 |
"Transfer-Encoding: chunked\r\n" |
| 229 |
"\r\n" |
| 230 |
"0\r\n" |
| 231 |
"X-Foo: ", Large |
| 232 |
]), |
| 233 |
Data = raw_recv_head(Client), |
| 234 |
{'HTTP/1.1', 400, _, _} = cow_http:parse_status_line(Data), |
| 235 |
{error, closed} = raw_recv(Client, 0, 1000). |
| 236 |
|
| 237 |
disable_http1_tls(Config) -> |
| 238 |
doc("Ensure that we can disable HTTP/1.1 over TLS (force HTTP/2)."), |
| 239 |
TlsOpts = ct_helper:get_certs_from_ets(), |
| 240 |
{ok, _} = cowboy:start_tls(?FUNCTION_NAME, TlsOpts ++ [{port, 0}], #{ |
| 241 |
env => #{dispatch => init_dispatch(Config)}, |
| 242 |
alpn_default_protocol => http2 |
| 243 |
}), |
| 244 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 245 |
try |
| 246 |
{ok, Socket} = ssl:connect("localhost", Port, |
| 247 |
[binary, {active, false}|TlsOpts]), |
| 248 |
%% ALPN was not negotiated but we're still over HTTP/2. |
| 249 |
{error, protocol_not_negotiated} = ssl:negotiated_protocol(Socket), |
| 250 |
%% Send a valid preface. |
| 251 |
ok = ssl:send(Socket, [ |
| 252 |
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", |
| 253 |
cow_http2:settings(#{})]), |
| 254 |
%% Receive the server preface. |
| 255 |
{ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000), |
| 256 |
{ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000), |
| 257 |
ok |
| 258 |
after |
| 259 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 260 |
end. |
| 261 |
|
| 262 |
disable_http2_prior_knowledge(Config) -> |
| 263 |
doc("Ensure that we can disable prior knowledge HTTP/2 upgrade."), |
| 264 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 265 |
env => #{dispatch => init_dispatch(Config)}, |
| 266 |
protocols => [http] |
| 267 |
}), |
| 268 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 269 |
try |
| 270 |
{ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}]), |
| 271 |
%% Send a valid preface. |
| 272 |
ok = gen_tcp:send(Socket, [ |
| 273 |
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", |
| 274 |
cow_http2:settings(#{})]), |
| 275 |
{ok, <<"HTTP/1.1 501">>} = gen_tcp:recv(Socket, 12, 1000), |
| 276 |
ok |
| 277 |
after |
| 278 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 279 |
end. |
| 280 |
|
| 281 |
disable_http2_upgrade(Config) -> |
| 282 |
doc("Ensure that we can disable HTTP/1.1 upgrade to HTTP/2."), |
| 283 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 284 |
env => #{dispatch => init_dispatch(Config)}, |
| 285 |
protocols => [http] |
| 286 |
}), |
| 287 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 288 |
try |
| 289 |
{ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}]), |
| 290 |
%% Send a valid preface. |
| 291 |
ok = gen_tcp:send(Socket, [ |
| 292 |
"GET / HTTP/1.1\r\n" |
| 293 |
"Host: localhost\r\n" |
| 294 |
"Connection: Upgrade, HTTP2-Settings\r\n" |
| 295 |
"Upgrade: h2c\r\n" |
| 296 |
"HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n", |
| 297 |
"\r\n"]), |
| 298 |
{ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000), |
| 299 |
ok |
| 300 |
after |
| 301 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 302 |
end. |
| 303 |
|
| 304 |
hibernate(Config) -> |
| 305 |
doc("Ensure that we can enable hibernation for HTTP/1.1 connections."), |
| 306 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 307 |
env => #{dispatch => init_dispatch(Config)}, |
| 308 |
hibernate => true |
| 309 |
}), |
| 310 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 311 |
try |
| 312 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 313 |
{ok, http} = gun:await_up(ConnPid), |
| 314 |
StreamRef1 = gun:get(ConnPid, "/"), |
| 315 |
StreamRef2 = gun:get(ConnPid, "/"), |
| 316 |
StreamRef3 = gun:get(ConnPid, "/"), |
| 317 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef1), |
| 318 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef2), |
| 319 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef3), |
| 320 |
gun:close(ConnPid) |
| 321 |
after |
| 322 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 323 |
end. |
| 324 |
|
| 325 |
http10_keepalive_false(Config) -> |
| 326 |
doc("Confirm the option http10_keepalive => false disables keep-alive " |
| 327 |
"completely for HTTP/1.0 connections."), |
| 328 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 329 |
env => #{dispatch => init_dispatch(Config)}, |
| 330 |
http10_keepalive => false |
| 331 |
}), |
| 332 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 333 |
try |
| 334 |
Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n", |
| 335 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 336 |
ok = raw_send(Client, Keepalive), |
| 337 |
Data = raw_recv_head(Client), |
| 338 |
%% Cowboy always advertises itself as HTTP/1.1. |
| 339 |
{'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data), |
| 340 |
{Headers, _} = cow_http:parse_headers(Rest), |
| 341 |
{_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers), |
| 342 |
ok = raw_send(Client, Keepalive), |
| 343 |
try raw_recv_head(Client) of |
| 344 |
_ -> error(not_closed) |
| 345 |
catch _:_ -> |
| 346 |
closed |
| 347 |
end |
| 348 |
after |
| 349 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 350 |
end. |
| 351 |
|
| 352 |
idle_timeout_read_body(Config) -> |
| 353 |
doc("Ensure the idle_timeout drops connections when the " |
| 354 |
"connection is idle too long reading the request body."), |
| 355 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 356 |
env => #{dispatch => init_dispatch(Config)}, |
| 357 |
request_timeout => 60000, |
| 358 |
idle_timeout => 500 |
| 359 |
}), |
| 360 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 361 |
try |
| 362 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 363 |
{ok, http} = gun:await_up(ConnPid), |
| 364 |
_StreamRef = gun:post(ConnPid, "/echo/read_body", |
| 365 |
#{<<"content-length">> => <<"12">>}), |
| 366 |
{error, {down, {shutdown, closed}}} = gun:await(ConnPid, undefined, 1000) |
| 367 |
after |
| 368 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 369 |
end. |
| 370 |
|
| 371 |
idle_timeout_read_body_pipeline(Config) -> |
| 372 |
doc("Ensure the idle_timeout drops connections when the " |
| 373 |
"connection is idle too long reading the request body."), |
| 374 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 375 |
env => #{dispatch => init_dispatch(Config)}, |
| 376 |
request_timeout => 60000, |
| 377 |
idle_timeout => 500 |
| 378 |
}), |
| 379 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 380 |
try |
| 381 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 382 |
{ok, http} = gun:await_up(ConnPid), |
| 383 |
StreamRef1 = gun:get(ConnPid, "/"), |
| 384 |
StreamRef2 = gun:get(ConnPid, "/"), |
| 385 |
_StreamRef3 = gun:post(ConnPid, "/echo/read_body", |
| 386 |
#{<<"content-length">> => <<"12">>}), |
| 387 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef1), |
| 388 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef2), |
| 389 |
{error, {down, {shutdown, closed}}} = gun:await(ConnPid, undefined, 1000) |
| 390 |
after |
| 391 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 392 |
end. |
| 393 |
|
| 394 |
idle_timeout_skip_body(Config) -> |
| 395 |
doc("Ensure the idle_timeout drops connections when the " |
| 396 |
"connection is idle too long skipping the request body."), |
| 397 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 398 |
env => #{dispatch => init_dispatch(Config)}, |
| 399 |
request_timeout => 60000, |
| 400 |
idle_timeout => 500 |
| 401 |
}), |
| 402 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 403 |
try |
| 404 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 405 |
{ok, http} = gun:await_up(ConnPid), |
| 406 |
StreamRef = gun:post(ConnPid, "/", |
| 407 |
#{<<"content-length">> => <<"12">>}), |
| 408 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef), |
| 409 |
{error, {down, {shutdown, closed}}} = gun:await(ConnPid, undefined, 1000) |
| 410 |
after |
| 411 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 412 |
end. |
| 413 |
|
| 414 |
idle_timeout_infinity(Config) -> |
| 415 |
doc("Ensure the idle_timeout option accepts the infinity value."), |
| 416 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 417 |
env => #{dispatch => init_dispatch(Config)}, |
| 418 |
idle_timeout => infinity |
| 419 |
}), |
| 420 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 421 |
try |
| 422 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 423 |
{ok, http} = gun:await_up(ConnPid), |
| 424 |
timer:sleep(500), |
| 425 |
#{socket := Socket} = gun:info(ConnPid), |
| 426 |
Pid = get_remote_pid_tcp(Socket), |
| 427 |
_ = gun:post(ConnPid, "/echo/read_body", |
| 428 |
[{<<"content-type">>, <<"text/plain">>}]), |
| 429 |
Ref = erlang:monitor(process, Pid), |
| 430 |
receive |
| 431 |
{'DOWN', Ref, process, Pid, Reason} -> |
| 432 |
error(Reason) |
| 433 |
after 1000 -> |
| 434 |
gun:close(ConnPid) |
| 435 |
end |
| 436 |
after |
| 437 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 438 |
end. |
| 439 |
|
| 440 |
idle_timeout_on_send(Config) -> |
| 441 |
doc("Ensure the idle timeout is not reset when sending (by default)."), |
| 442 |
do_idle_timeout_on_send(Config, http). |
| 443 |
|
| 444 |
%% Also used by http2_SUITE. |
| 445 |
do_idle_timeout_on_send(Config, Protocol) -> |
| 446 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 447 |
env => #{dispatch => init_dispatch(Config)}, |
| 448 |
idle_timeout => 1000 |
| 449 |
}), |
| 450 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 451 |
try |
| 452 |
ConnPid = gun_open([{type, tcp}, {protocol, Protocol}, {port, Port}|Config]), |
| 453 |
{ok, Protocol} = gun:await_up(ConnPid), |
| 454 |
timer:sleep(500), |
| 455 |
#{socket := Socket} = gun:info(ConnPid), |
| 456 |
Pid = get_remote_pid_tcp(Socket), |
| 457 |
StreamRef = gun:get(ConnPid, "/streamed_result/10/250"), |
| 458 |
Ref = erlang:monitor(process, Pid), |
| 459 |
receive |
| 460 |
{gun_response, ConnPid, StreamRef, nofin, _Status, _Headers} -> |
| 461 |
do_idle_timeout_recv_loop(Ref, Pid, ConnPid, StreamRef, false) |
| 462 |
after 2000 -> |
| 463 |
error(timeout) |
| 464 |
end |
| 465 |
after |
| 466 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 467 |
end. |
| 468 |
|
| 469 |
idle_timeout_reset_on_send(Config) -> |
| 470 |
doc("Ensure the reset_idle_timeout_on_send results in the " |
| 471 |
"idle timeout resetting when sending ."), |
| 472 |
do_idle_timeout_reset_on_send(Config, http). |
| 473 |
|
| 474 |
%% Also used by http2_SUITE. |
| 475 |
do_idle_timeout_reset_on_send(Config, Protocol) -> |
| 476 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 477 |
env => #{dispatch => init_dispatch(Config)}, |
| 478 |
idle_timeout => 1000, |
| 479 |
reset_idle_timeout_on_send => true |
| 480 |
}), |
| 481 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 482 |
try |
| 483 |
ConnPid = gun_open([{type, tcp}, {protocol, Protocol}, {port, Port}|Config]), |
| 484 |
{ok, Protocol} = gun:await_up(ConnPid), |
| 485 |
timer:sleep(500), |
| 486 |
#{socket := Socket} = gun:info(ConnPid), |
| 487 |
Pid = get_remote_pid_tcp(Socket), |
| 488 |
StreamRef = gun:get(ConnPid, "/streamed_result/10/250"), |
| 489 |
Ref = erlang:monitor(process, Pid), |
| 490 |
receive |
| 491 |
{gun_response, ConnPid, StreamRef, nofin, _Status, _Headers} -> |
| 492 |
do_idle_timeout_recv_loop(Ref, Pid, ConnPid, StreamRef, true) |
| 493 |
after 2000 -> |
| 494 |
error(timeout) |
| 495 |
end |
| 496 |
after |
| 497 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 498 |
end. |
| 499 |
|
| 500 |
do_idle_timeout_recv_loop(Ref, Pid, ConnPid, StreamRef, ExpectCompletion) -> |
| 501 |
receive |
| 502 |
{gun_data, ConnPid, StreamRef, nofin, _Data} -> |
| 503 |
do_idle_timeout_recv_loop(Ref, Pid, ConnPid, StreamRef, ExpectCompletion); |
| 504 |
{gun_data, ConnPid, StreamRef, fin, _Data} when ExpectCompletion -> |
| 505 |
gun:close(ConnPid); |
| 506 |
{gun_data, ConnPid, StreamRef, fin, _Data} -> |
| 507 |
gun:close(ConnPid), |
| 508 |
error(completed); |
| 509 |
{'DOWN', Ref, process, Pid, _} when ExpectCompletion -> |
| 510 |
gun:close(ConnPid), |
| 511 |
error(exited); |
| 512 |
{'DOWN', Ref, process, Pid, _} -> |
| 513 |
ok |
| 514 |
after 2000 -> |
| 515 |
error(timeout) |
| 516 |
end. |
| 517 |
|
| 518 |
invalid_response_headers_inform(Config) -> |
| 519 |
doc("Ensure invalid response headers are rejected by default."), |
| 520 |
Dispatch = cowboy_router:compile([{'_', [ |
| 521 |
{"/inform", resp_invalid_headers_h, []} |
| 522 |
]}]), |
| 523 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 524 |
env => #{dispatch => Dispatch} |
| 525 |
}), |
| 526 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 527 |
try |
| 528 |
Request = "GET /inform HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 529 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 530 |
ok = raw_send(Client, Request), |
| 531 |
{'HTTP/1.1', 500, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 532 |
{_, _} = cow_http:parse_headers(Rest), |
| 533 |
ok |
| 534 |
after |
| 535 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 536 |
end. |
| 537 |
|
| 538 |
invalid_response_headers_inform_ignore(Config) -> |
| 539 |
doc("Ensure invalid response headers are sent " |
| 540 |
"when allowed by configuration."), |
| 541 |
Dispatch = cowboy_router:compile([{'_', [ |
| 542 |
{"/inform", resp_invalid_headers_h, []} |
| 543 |
]}]), |
| 544 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 545 |
env => #{dispatch => Dispatch}, |
| 546 |
invalid_response_headers => ignore |
| 547 |
}), |
| 548 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 549 |
try |
| 550 |
Request = "GET /inform HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 551 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 552 |
ok = raw_send(Client, Request), |
| 553 |
{'HTTP/1.1', 100, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 554 |
?assertError(function_clause, cow_http:parse_headers(Rest), |
| 555 |
"Invalid header goes through."), |
| 556 |
{'HTTP/1.1', 200, _, _} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 557 |
ok |
| 558 |
after |
| 559 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 560 |
end. |
| 561 |
|
| 562 |
invalid_response_headers_reply(Config) -> |
| 563 |
doc("Ensure invalid response headers are rejected by default."), |
| 564 |
Dispatch = cowboy_router:compile([{'_', [ |
| 565 |
{"/reply", resp_invalid_headers_h, []} |
| 566 |
]}]), |
| 567 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 568 |
env => #{dispatch => Dispatch} |
| 569 |
}), |
| 570 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 571 |
try |
| 572 |
Request = "GET /reply HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 573 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 574 |
ok = raw_send(Client, Request), |
| 575 |
{'HTTP/1.1', 500, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 576 |
{_, _} = cow_http:parse_headers(Rest), |
| 577 |
ok |
| 578 |
after |
| 579 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 580 |
end. |
| 581 |
|
| 582 |
invalid_response_headers_reply_ignore(Config) -> |
| 583 |
doc("Ensure invalid response headers are sent " |
| 584 |
"when allowed by configuration."), |
| 585 |
Dispatch = cowboy_router:compile([{'_', [ |
| 586 |
{"/reply", resp_invalid_headers_h, []} |
| 587 |
]}]), |
| 588 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 589 |
env => #{dispatch => Dispatch}, |
| 590 |
invalid_response_headers => ignore |
| 591 |
}), |
| 592 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 593 |
try |
| 594 |
Request = "GET /reply HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 595 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 596 |
ok = raw_send(Client, Request), |
| 597 |
{'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 598 |
?assertError(function_clause, cow_http:parse_headers(Rest), |
| 599 |
"Invalid header goes through.") |
| 600 |
after |
| 601 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 602 |
end. |
| 603 |
|
| 604 |
invalid_response_headers_stream_reply(Config) -> |
| 605 |
doc("Ensure invalid response headers are rejected by default."), |
| 606 |
Dispatch = cowboy_router:compile([{'_', [ |
| 607 |
{"/stream_reply", resp_invalid_headers_h, []} |
| 608 |
]}]), |
| 609 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 610 |
env => #{dispatch => Dispatch}, |
| 611 |
invalid_response_headers => error_terminate |
| 612 |
}), |
| 613 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 614 |
try |
| 615 |
Request = "GET /stream_reply HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 616 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 617 |
ok = raw_send(Client, Request), |
| 618 |
{'HTTP/1.1', 500, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 619 |
{_, _} = cow_http:parse_headers(Rest), |
| 620 |
ok |
| 621 |
after |
| 622 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 623 |
end. |
| 624 |
|
| 625 |
invalid_response_headers_stream_reply_ignore(Config) -> |
| 626 |
doc("Ensure invalid response headers are sent " |
| 627 |
"when allowed by configuration."), |
| 628 |
Dispatch = cowboy_router:compile([{'_', [ |
| 629 |
{"/stream_reply", resp_invalid_headers_h, []} |
| 630 |
]}]), |
| 631 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 632 |
env => #{dispatch => Dispatch}, |
| 633 |
invalid_response_headers => ignore |
| 634 |
}), |
| 635 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 636 |
try |
| 637 |
Request = "GET /stream_reply HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 638 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 639 |
ok = raw_send(Client, Request), |
| 640 |
{'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 641 |
?assertError(function_clause, cow_http:parse_headers(Rest), |
| 642 |
"Invalid header goes through.") |
| 643 |
after |
| 644 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 645 |
end. |
| 646 |
|
| 647 |
invalid_response_headers_stream_trailers(Config) -> |
| 648 |
doc("Ensure invalid response headers are rejected by default."), |
| 649 |
Dispatch = cowboy_router:compile([{'_', [ |
| 650 |
{"/stream_trailers", resp_invalid_headers_h, []} |
| 651 |
]}]), |
| 652 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 653 |
env => #{dispatch => Dispatch}, |
| 654 |
invalid_response_headers => error_terminate |
| 655 |
}), |
| 656 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 657 |
try |
| 658 |
Request = "GET /stream_trailers HTTP/1.1\r\nhost: localhost\r\n" |
| 659 |
"te: trailers\r\n\r\n", |
| 660 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 661 |
ok = raw_send(Client, Request), |
| 662 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 663 |
{Headers, <<>>} = cow_http:parse_headers(Rest0), |
| 664 |
{_, _} = lists:keyfind(<<"trailer">>, 1, Headers), |
| 665 |
{ok, <<"2\r\nOK\r\n">>} = raw_recv(Client, 0, 1000), |
| 666 |
%% Connection should be closed without trailers. |
| 667 |
{error, closed} = raw_recv(Client, 0, 1000) |
| 668 |
after |
| 669 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 670 |
end. |
| 671 |
|
| 672 |
invalid_response_headers_stream_trailers_ignore(Config) -> |
| 673 |
doc("Ensure invalid response headers are sent " |
| 674 |
"when allowed by configuration."), |
| 675 |
Dispatch = cowboy_router:compile([{'_', [ |
| 676 |
{"/stream_trailers", resp_invalid_headers_h, []} |
| 677 |
]}]), |
| 678 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 679 |
env => #{dispatch => Dispatch}, |
| 680 |
invalid_response_headers => ignore |
| 681 |
}), |
| 682 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 683 |
try |
| 684 |
Request = "GET /stream_trailers HTTP/1.1\r\nhost: localhost\r\n" |
| 685 |
"te: trailers\r\n\r\n", |
| 686 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 687 |
ok = raw_send(Client, Request), |
| 688 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 689 |
{Headers, <<>>} = cow_http:parse_headers(Rest0), |
| 690 |
{_, _} = lists:keyfind(<<"trailer">>, 1, Headers), |
| 691 |
{ok, <<"2\r\nOK\r\n">>} = raw_recv(Client, 0, 1000), |
| 692 |
{ok, Rest1} = raw_recv(Client, 0, 1000), |
| 693 |
?assertError(function_clause, cow_http:parse_headers(Rest1), |
| 694 |
"Invalid header goes through.") |
| 695 |
after |
| 696 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 697 |
end. |
| 698 |
|
| 699 |
invalid_response_headers_switch_protocol(Config) -> |
| 700 |
doc("Ensure invalid response headers are rejected by default."), |
| 701 |
Dispatch = cowboy_router:compile([{'_', [ |
| 702 |
{"/switch_protocol", resp_invalid_headers_h, []} |
| 703 |
]}]), |
| 704 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 705 |
env => #{dispatch => Dispatch} |
| 706 |
}), |
| 707 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 708 |
try |
| 709 |
Request = "GET /switch_protocol HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 710 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 711 |
ok = raw_send(Client, Request), |
| 712 |
{'HTTP/1.1', 500, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 713 |
{_, _} = cow_http:parse_headers(Rest), |
| 714 |
ok |
| 715 |
after |
| 716 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 717 |
end. |
| 718 |
|
| 719 |
invalid_response_headers_switch_protocol_ignore(Config) -> |
| 720 |
doc("Ensure invalid response headers are sent " |
| 721 |
"when allowed by configuration."), |
| 722 |
Dispatch = cowboy_router:compile([{'_', [ |
| 723 |
{"/switch_protocol", resp_invalid_headers_h, []} |
| 724 |
]}]), |
| 725 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 726 |
env => #{dispatch => Dispatch}, |
| 727 |
invalid_response_headers => ignore |
| 728 |
}), |
| 729 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 730 |
try |
| 731 |
Request = "GET /switch_protocol HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 732 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 733 |
ok = raw_send(Client, Request), |
| 734 |
{'HTTP/1.1', 101, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 735 |
?assertError(function_clause, cow_http:parse_headers(Rest), |
| 736 |
"Invalid header goes through.") |
| 737 |
after |
| 738 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 739 |
end. |
| 740 |
|
| 741 |
invalid_response_headers_early_error(Config) -> |
| 742 |
doc("Ensure invalid response headers are rejected by default."), |
| 743 |
Dispatch = cowboy_router:compile([{'_', []}]), |
| 744 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 745 |
env => #{dispatch => Dispatch}, |
| 746 |
stream_handlers => [stream_handler_invalid_headers_h] |
| 747 |
}), |
| 748 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 749 |
try |
| 750 |
Request = "TRACE / HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 751 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 752 |
ok = raw_send(Client, Request), |
| 753 |
%% We sent the initial response, ignoring the |
| 754 |
%% modified response from stream handlers. |
| 755 |
{'HTTP/1.1', 501, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 756 |
{_, _} = cow_http:parse_headers(Rest), |
| 757 |
ok |
| 758 |
after |
| 759 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 760 |
end. |
| 761 |
|
| 762 |
invalid_response_headers_early_error_ignore(Config) -> |
| 763 |
doc("Ensure invalid response headers are sent " |
| 764 |
"when allowed by configuration."), |
| 765 |
Dispatch = cowboy_router:compile([{'_', []}]), |
| 766 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 767 |
env => #{dispatch => Dispatch}, |
| 768 |
invalid_response_headers => ignore, |
| 769 |
stream_handlers => [stream_handler_invalid_headers_h] |
| 770 |
}), |
| 771 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 772 |
try |
| 773 |
Request = "TRACE /switch_protocol HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 774 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 775 |
ok = raw_send(Client, Request), |
| 776 |
{'HTTP/1.1', 501, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 777 |
?assertError(function_clause, cow_http:parse_headers(Rest), |
| 778 |
"Invalid header goes through.") |
| 779 |
after |
| 780 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 781 |
end. |
| 782 |
|
| 783 |
max_authority_length_host_header(Config) -> |
| 784 |
doc("Confirm the max_authority_length option correctly limits " |
| 785 |
"the length of the authority component in the host header."), |
| 786 |
Dispatch = cowboy_router:compile([{'_', [{"/", hello_h, []}]}]), |
| 787 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 788 |
env => #{dispatch => Dispatch}, |
| 789 |
max_authority_length => 10 |
| 790 |
}), |
| 791 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 792 |
try |
| 793 |
RawConfig = [{port, Port}, {type, tcp}, {opts, []}|Config], |
| 794 |
%% At limit. |
| 795 |
#{code := 200} = rfc7230_SUITE:do_raw(RawConfig, |
| 796 |
"GET / HTTP/1.1\r\n" |
| 797 |
"Host: example.co\r\n" |
| 798 |
"\r\n"), |
| 799 |
%% Over limit. |
| 800 |
#{code := 414, client := Client} = rfc7230_SUITE:do_raw(RawConfig, |
| 801 |
"GET / HTTP/1.1\r\n" |
| 802 |
"Host: example.com\r\n" |
| 803 |
"\r\n"), |
| 804 |
{error, closed} = raw_recv(Client, 0, 1000) |
| 805 |
after |
| 806 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 807 |
end. |
| 808 |
|
| 809 |
max_authority_length_request_line(Config) -> |
| 810 |
doc("Confirm the max_authority_length option correctly limits " |
| 811 |
"the length of the authority component in the absolute-form " |
| 812 |
"target of the request line."), |
| 813 |
Dispatch = cowboy_router:compile([{'_', [{"/", hello_h, []}]}]), |
| 814 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 815 |
env => #{dispatch => Dispatch}, |
| 816 |
max_authority_length => 10 |
| 817 |
}), |
| 818 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 819 |
try |
| 820 |
RawConfig = [{port, Port}, {type, tcp}, {opts, []}|Config], |
| 821 |
%% At limit. |
| 822 |
#{code := 200} = rfc7230_SUITE:do_raw(RawConfig, |
| 823 |
"GET http://example.co/ HTTP/1.1\r\n" |
| 824 |
"Host: example.co\r\n" |
| 825 |
"\r\n"), |
| 826 |
%% Over limit. |
| 827 |
#{code := 414, client := Client} = rfc7230_SUITE:do_raw(RawConfig, |
| 828 |
"GET http://example.com/ HTTP/1.1\r\n" |
| 829 |
"Host: example.com\r\n" |
| 830 |
"\r\n"), |
| 831 |
{error, closed} = raw_recv(Client, 0, 1000) |
| 832 |
after |
| 833 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 834 |
end. |
| 835 |
|
| 836 |
max_authorization_header_value_length(Config) -> |
| 837 |
doc("Confirm the max_authorization_header_value_length option " |
| 838 |
"correctly limits the length of authorization header values."), |
| 839 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 840 |
env => #{dispatch => init_dispatch(Config)}, |
| 841 |
max_authorization_header_value_length => 2048 |
| 842 |
}), |
| 843 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 844 |
try |
| 845 |
do_max_header_value_length(Config, Port, |
| 846 |
<<"authorization">>, 2048), |
| 847 |
%% Confirm that other headers still use the default limit. |
| 848 |
do_max_header_value_length(Config, Port, |
| 849 |
<<"my-header">>, 4096) |
| 850 |
after |
| 851 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 852 |
end. |
| 853 |
|
| 854 |
max_cookie_header_value_length(Config) -> |
| 855 |
doc("Confirm the max_cookie_header_value_length option " |
| 856 |
"correctly limits the length of cookie header values."), |
| 857 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 858 |
env => #{dispatch => init_dispatch(Config)}, |
| 859 |
max_cookie_header_value_length => 2048 |
| 860 |
}), |
| 861 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 862 |
try |
| 863 |
do_max_header_value_length(Config, Port, |
| 864 |
<<"cookie">>, 2048), |
| 865 |
%% Confirm that other headers still use the default limit. |
| 866 |
do_max_header_value_length(Config, Port, |
| 867 |
<<"my-header">>, 4096) |
| 868 |
after |
| 869 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 870 |
end. |
| 871 |
|
| 872 |
max_header_value_length(Config) -> |
| 873 |
doc("Confirm the max_header_value_length option " |
| 874 |
"correctly limits the length of header values."), |
| 875 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 876 |
env => #{dispatch => init_dispatch(Config)}, |
| 877 |
max_header_value_length => 2048 |
| 878 |
}), |
| 879 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 880 |
try |
| 881 |
do_max_header_value_length(Config, Port, |
| 882 |
<<"my-header">>, 2048) |
| 883 |
after |
| 884 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 885 |
end. |
| 886 |
|
| 887 |
max_header_value_length_default(Config) -> |
| 888 |
doc("Confirm the max_header_value_length option " |
| 889 |
"correctly limits the length of header values."), |
| 890 |
do_max_header_value_length(Config, config(port, Config), |
| 891 |
<<"my-header">>, 4096). |
| 892 |
|
| 893 |
do_max_header_value_length(Config, Port, Name, MaxLen) -> |
| 894 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 895 |
{ok, http} = gun:await_up(ConnPid), |
| 896 |
StreamRef1 = gun:get(ConnPid, "/", #{Name => lists:duplicate(MaxLen, $a)}), |
| 897 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef1), |
| 898 |
%% We * 2 because this is a soft limit. |
| 899 |
StreamRef2 = gun:get(ConnPid, "/", #{Name => lists:duplicate(MaxLen * 2, $a)}), |
| 900 |
{response, fin, 431, _} = gun:await(ConnPid, StreamRef2), |
| 901 |
gun:close(ConnPid). |
| 902 |
|
| 903 |
persistent_term_router(Config) -> |
| 904 |
doc("The router can retrieve the routes from persistent_term storage."), |
| 905 |
case erlang:function_exported(persistent_term, get, 1) of |
| 906 |
true -> do_persistent_term_router(Config); |
| 907 |
false -> {skip, "This test uses the persistent_term functionality added in Erlang/OTP 21.2."} |
| 908 |
end. |
| 909 |
|
| 910 |
do_persistent_term_router(Config) -> |
| 911 |
persistent_term:put(?FUNCTION_NAME, init_dispatch(Config)), |
| 912 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 913 |
env => #{dispatch => {persistent_term, ?FUNCTION_NAME}} |
| 914 |
}), |
| 915 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 916 |
try |
| 917 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 918 |
{ok, http} = gun:await_up(ConnPid), |
| 919 |
StreamRef = gun:get(ConnPid, "/"), |
| 920 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef), |
| 921 |
gun:close(ConnPid) |
| 922 |
after |
| 923 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 924 |
end. |
| 925 |
|
| 926 |
request_timeout(Config) -> |
| 927 |
doc("Ensure the request_timeout drops connections when requests " |
| 928 |
"fail to come in fast enough."), |
| 929 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 930 |
env => #{dispatch => init_dispatch(Config)}, |
| 931 |
request_timeout => 500 |
| 932 |
}), |
| 933 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 934 |
try |
| 935 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 936 |
{ok, http} = gun:await_up(ConnPid), |
| 937 |
{error, {down, {shutdown, closed}}} = gun:await(ConnPid, undefined, 1000) |
| 938 |
after |
| 939 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 940 |
end. |
| 941 |
|
| 942 |
request_timeout_pipeline(Config) -> |
| 943 |
doc("Ensure the request_timeout drops connections when requests " |
| 944 |
"fail to come in fast enough after pipelined requests went through."), |
| 945 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 946 |
env => #{dispatch => init_dispatch(Config)}, |
| 947 |
request_timeout => 500 |
| 948 |
}), |
| 949 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 950 |
try |
| 951 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 952 |
{ok, http} = gun:await_up(ConnPid), |
| 953 |
StreamRef1 = gun:get(ConnPid, "/"), |
| 954 |
StreamRef2 = gun:get(ConnPid, "/"), |
| 955 |
StreamRef3 = gun:get(ConnPid, "/"), |
| 956 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef1), |
| 957 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef2), |
| 958 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef3), |
| 959 |
{error, {down, {shutdown, closed}}} = gun:await(ConnPid, undefined, 1000) |
| 960 |
after |
| 961 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 962 |
end. |
| 963 |
|
| 964 |
request_timeout_pipeline_delay(Config) -> |
| 965 |
doc("Ensure the request_timeout does not trigger on requests " |
| 966 |
"coming in after a large request body."), |
| 967 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 968 |
env => #{dispatch => init_dispatch(Config)}, |
| 969 |
request_timeout => 500 |
| 970 |
}), |
| 971 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 972 |
try |
| 973 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 974 |
{ok, http} = gun:await_up(ConnPid), |
| 975 |
StreamRef1 = gun:post(ConnPid, "/", #{}, <<0:8000000>>), |
| 976 |
StreamRef2 = gun:get(ConnPid, "/delay_hello"), |
| 977 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef1), |
| 978 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef2), |
| 979 |
{error, {down, {shutdown, closed}}} = gun:await(ConnPid, undefined, 1000) |
| 980 |
after |
| 981 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 982 |
end. |
| 983 |
|
| 984 |
request_timeout_skip_body(Config) -> |
| 985 |
doc("Ensure the request_timeout drops connections when requests " |
| 986 |
"fail to come in fast enough after skipping a request body."), |
| 987 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 988 |
env => #{dispatch => init_dispatch(Config)}, |
| 989 |
request_timeout => 500 |
| 990 |
}), |
| 991 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 992 |
try |
| 993 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 994 |
ok = raw_send(Client, << |
| 995 |
"POST / HTTP/1.1\r\n" |
| 996 |
"host: localhost\r\n" |
| 997 |
"content-length: 12\r\n\r\n" |
| 998 |
>>), |
| 999 |
Data = raw_recv_head(Client), |
| 1000 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data), |
| 1001 |
{Headers, Rest} = cow_http:parse_headers(Rest0), |
| 1002 |
{_, Len} = lists:keyfind(<<"content-length">>, 1, Headers), |
| 1003 |
<<"Hello world!">> = raw_recv_rest(Client, binary_to_integer(Len), Rest), |
| 1004 |
%% We then send the request data that should be skipped by Cowboy. |
| 1005 |
timer:sleep(100), |
| 1006 |
raw_send(Client, <<"Hello world!">>), |
| 1007 |
%% Connection should be closed by the request_timeout after that. |
| 1008 |
{error, closed} = raw_recv(Client, 1, 1000) |
| 1009 |
after |
| 1010 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 1011 |
end. |
| 1012 |
|
| 1013 |
request_timeout_skip_body_more(Config) -> |
| 1014 |
doc("Ensure the request_timeout drops connections when requests " |
| 1015 |
"fail to come in fast enough after skipping a request body."), |
| 1016 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 1017 |
env => #{dispatch => init_dispatch(Config)}, |
| 1018 |
request_timeout => 500 |
| 1019 |
}), |
| 1020 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 1021 |
try |
| 1022 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 1023 |
ok = raw_send(Client, << |
| 1024 |
"POST / HTTP/1.1\r\n" |
| 1025 |
"host: localhost\r\n" |
| 1026 |
"content-length: 12\r\n\r\n" |
| 1027 |
>>), |
| 1028 |
Data = raw_recv_head(Client), |
| 1029 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data), |
| 1030 |
{Headers, Rest} = cow_http:parse_headers(Rest0), |
| 1031 |
{_, Len} = lists:keyfind(<<"content-length">>, 1, Headers), |
| 1032 |
<<"Hello world!">> = raw_recv_rest(Client, binary_to_integer(Len), Rest), |
| 1033 |
%% We then send the request data that should be skipped by Cowboy. |
| 1034 |
timer:sleep(100), |
| 1035 |
raw_send(Client, <<"Hello world!">>), |
| 1036 |
%% Send the start of another request. |
| 1037 |
ok = raw_send(Client, << |
| 1038 |
"GET / HTTP/1.1\r\n" |
| 1039 |
"host: localhost\r\n" |
| 1040 |
%% Missing final \r\n on purpose. |
| 1041 |
>>), |
| 1042 |
%% Connection should be closed by the request_timeout after that. |
| 1043 |
%% We attempt to send a 408 response on a best effort basis so |
| 1044 |
%% that is accepted as well. |
| 1045 |
case raw_recv(Client, 13, 1000) of |
| 1046 |
{error, closed} -> ok; |
| 1047 |
{ok, <<"HTTP/1.1 408 ", _/bits>>} -> ok |
| 1048 |
end |
| 1049 |
after |
| 1050 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 1051 |
end. |
| 1052 |
|
| 1053 |
request_timeout_infinity(Config) -> |
| 1054 |
doc("Ensure the request_timeout option accepts the infinity value."), |
| 1055 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 1056 |
env => #{dispatch => init_dispatch(Config)}, |
| 1057 |
request_timeout => infinity |
| 1058 |
}), |
| 1059 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 1060 |
try |
| 1061 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 1062 |
{ok, http} = gun:await_up(ConnPid), |
| 1063 |
timer:sleep(500), |
| 1064 |
#{socket := Socket} = gun:info(ConnPid), |
| 1065 |
Pid = get_remote_pid_tcp(Socket), |
| 1066 |
Ref = erlang:monitor(process, Pid), |
| 1067 |
receive |
| 1068 |
{'DOWN', Ref, process, Pid, Reason} -> |
| 1069 |
error(Reason) |
| 1070 |
after 1000 -> |
| 1071 |
gun:close(ConnPid) |
| 1072 |
end |
| 1073 |
after |
| 1074 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 1075 |
end. |
| 1076 |
|
| 1077 |
set_options_chunked_false(Config) -> |
| 1078 |
doc("Confirm the option chunked can be dynamically set to disable " |
| 1079 |
"chunked transfer-encoding. This results in the closing of the " |
| 1080 |
"connection after the current request."), |
| 1081 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 1082 |
env => #{dispatch => init_dispatch(Config)}, |
| 1083 |
chunked => true |
| 1084 |
}), |
| 1085 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 1086 |
try |
| 1087 |
Request = "GET /set_options/chunked_false HTTP/1.1\r\nhost: localhost\r\n\r\n", |
| 1088 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 1089 |
ok = raw_send(Client, Request), |
| 1090 |
Data = raw_recv_head(Client), |
| 1091 |
%% Cowboy always advertises itself as HTTP/1.1. |
| 1092 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data), |
| 1093 |
{Headers, Rest} = cow_http:parse_headers(Rest0), |
| 1094 |
false = lists:keyfind(<<"content-length">>, 1, Headers), |
| 1095 |
false = lists:keyfind(<<"transfer-encoding">>, 1, Headers), |
| 1096 |
Bits = 8000000 - bit_size(Rest), |
| 1097 |
raw_expect_recv(Client, <<0:Bits>>), |
| 1098 |
{error, closed} = raw_recv(Client, 1, 1000) |
| 1099 |
after |
| 1100 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 1101 |
end. |
| 1102 |
|
| 1103 |
set_options_chunked_false_ignored(Config) -> |
| 1104 |
doc("Confirm the option chunked can be dynamically set to disable " |
| 1105 |
"chunked transfer-encoding, and that it is ignored if the " |
| 1106 |
"response is not streamed."), |
| 1107 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 1108 |
env => #{dispatch => init_dispatch(Config)}, |
| 1109 |
chunked => true |
| 1110 |
}), |
| 1111 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 1112 |
try |
| 1113 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 1114 |
%% We do a first request setting the option but not |
| 1115 |
%% using chunked transfer-encoding in the response. |
| 1116 |
StreamRef1 = gun:get(ConnPid, "/set_options/chunked_false_ignored"), |
| 1117 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef1), |
| 1118 |
{ok, <<"Hello world!">>} = gun:await_body(ConnPid, StreamRef1), |
| 1119 |
%% We then do a second request to confirm that chunked |
| 1120 |
%% is not disabled for that second request. |
| 1121 |
StreamRef2 = gun:get(ConnPid, "/resp/stream_reply2/200"), |
| 1122 |
{response, nofin, 200, Headers} = gun:await(ConnPid, StreamRef2), |
| 1123 |
{_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers), |
| 1124 |
gun:close(ConnPid) |
| 1125 |
after |
| 1126 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 1127 |
end. |
| 1128 |
|
| 1129 |
set_options_idle_timeout(Config) -> |
| 1130 |
doc("Confirm that the idle_timeout option can be dynamically " |
| 1131 |
"set to change how long Cowboy will wait before it closes the connection."), |
| 1132 |
%% We start with a long timeout and then cut it short. |
| 1133 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 1134 |
env => #{dispatch => init_dispatch(Config)}, |
| 1135 |
idle_timeout => 60000 |
| 1136 |
}), |
| 1137 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 1138 |
try |
| 1139 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 1140 |
{ok, http} = gun:await_up(ConnPid), |
| 1141 |
timer:sleep(500), |
| 1142 |
#{socket := Socket} = gun:info(ConnPid), |
| 1143 |
Pid = get_remote_pid_tcp(Socket), |
| 1144 |
_ = gun:post(ConnPid, "/set_options/idle_timeout_short", |
| 1145 |
[{<<"content-type">>, <<"text/plain">>}]), |
| 1146 |
Ref = erlang:monitor(process, Pid), |
| 1147 |
receive |
| 1148 |
{'DOWN', Ref, process, Pid, _} -> |
| 1149 |
ok |
| 1150 |
after 2000 -> |
| 1151 |
error(timeout) |
| 1152 |
end |
| 1153 |
after |
| 1154 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 1155 |
end. |
| 1156 |
|
| 1157 |
set_options_idle_timeout_only_applies_to_current_request(Config) -> |
| 1158 |
doc("Confirm that changes to the idle_timeout option only apply to the current stream."), |
| 1159 |
%% We start with a long timeout and then cut it short. |
| 1160 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{ |
| 1161 |
env => #{dispatch => init_dispatch(Config)}, |
| 1162 |
idle_timeout => 500 |
| 1163 |
}), |
| 1164 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 1165 |
try |
| 1166 |
ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 1167 |
{ok, http} = gun:await_up(ConnPid), |
| 1168 |
timer:sleep(500), |
| 1169 |
#{socket := Socket} = gun:info(ConnPid), |
| 1170 |
Pid = get_remote_pid_tcp(Socket), |
| 1171 |
StreamRef = gun:post(ConnPid, "/set_options/idle_timeout_long", |
| 1172 |
[{<<"content-type">>, <<"text/plain">>}]), |
| 1173 |
Ref = erlang:monitor(process, Pid), |
| 1174 |
receive |
| 1175 |
{'DOWN', Ref, process, Pid, Reason} -> |
| 1176 |
error(Reason) |
| 1177 |
after 2000 -> |
| 1178 |
ok |
| 1179 |
end, |
| 1180 |
%% Finish the first request and start a second one to confirm |
| 1181 |
%% the idle_timeout option is back to normal. |
| 1182 |
gun:data(ConnPid, StreamRef, fin, <<"Hello!">>), |
| 1183 |
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef), |
| 1184 |
{ok, <<"Hello!">>} = gun:await_body(ConnPid, StreamRef), |
| 1185 |
_ = gun:post(ConnPid, "/echo/read_body", |
| 1186 |
[{<<"content-type">>, <<"text/plain">>}]), |
| 1187 |
receive |
| 1188 |
{'DOWN', Ref, process, Pid, _} -> |
| 1189 |
ok |
| 1190 |
after 2000 -> |
| 1191 |
error(timeout) |
| 1192 |
end |
| 1193 |
after |
| 1194 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 1195 |
end. |
| 1196 |
|
| 1197 |
switch_protocol_flush(Config) -> |
| 1198 |
doc("Confirm that switch_protocol does not flush unrelated messages."), |
| 1199 |
ProtoOpts = #{ |
| 1200 |
env => #{dispatch => init_dispatch(Config)}, |
| 1201 |
stream_handlers => [switch_protocol_flush_h] |
| 1202 |
}, |
| 1203 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 1204 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 1205 |
try |
| 1206 |
Self = self(), |
| 1207 |
ConnPid = gun_open([{port, Port}, {type, tcp}, {protocol, http}|Config]), |
| 1208 |
_ = gun:get(ConnPid, "/", [ |
| 1209 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 1210 |
]), |
| 1211 |
receive |
| 1212 |
{Self, Events} -> |
| 1213 |
switch_protocol_flush_h:validate(Events) |
| 1214 |
after 5000 -> |
| 1215 |
error(timeout) |
| 1216 |
end |
| 1217 |
after |
| 1218 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 1219 |
end. |
| 1220 |
|
| 1221 |
graceful_shutdown_connection(Config) -> |
| 1222 |
doc("Check that the current request is handled before gracefully " |
| 1223 |
"shutting down a connection."), |
| 1224 |
Dispatch = cowboy_router:compile([{"localhost", [ |
| 1225 |
{"/hello", hello_h, #{}}, |
| 1226 |
{"/delay_hello", delay_hello_h, |
| 1227 |
#{delay => 1000, notify_received => self()}} |
| 1228 |
]}]), |
| 1229 |
ProtoOpts = #{ |
| 1230 |
env => #{dispatch => Dispatch} |
| 1231 |
}, |
| 1232 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts), |
| 1233 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 1234 |
try |
| 1235 |
Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]), |
| 1236 |
ok = raw_send(Client, |
| 1237 |
"GET /delay_hello HTTP/1.1\r\n" |
| 1238 |
"Host: localhost\r\n\r\n" |
| 1239 |
"GET /hello HTTP/1.1\r\n" |
| 1240 |
"Host: localhost\r\n\r\n"), |
| 1241 |
receive {request_received, <<"/delay_hello">>} -> ok end, |
| 1242 |
CowboyConnPid = get_remote_pid_tcp(element(2, Client)), |
| 1243 |
CowboyConnRef = erlang:monitor(process, CowboyConnPid), |
| 1244 |
ok = sys:terminate(CowboyConnPid, system_is_going_down), |
| 1245 |
Data = raw_recv_head(Client), |
| 1246 |
{'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data), |
| 1247 |
{Headers, Rest} = cow_http:parse_headers(Rest0), |
| 1248 |
<<"close">> = proplists:get_value(<<"connection">>, Headers), |
| 1249 |
<<"Hello world!">> = raw_recv_rest(Client, byte_size(<<"Hello world!">>), Rest), |
| 1250 |
{error, closed} = raw_recv(Client, 0, 1000), |
| 1251 |
receive |
| 1252 |
{'DOWN', CowboyConnRef, process, CowboyConnPid, _Reason} -> |
| 1253 |
ok |
| 1254 |
end |
| 1255 |
after |
| 1256 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 1257 |
end. |
| 1258 |
|
| 1259 |
graceful_shutdown_listener(Config) -> |
| 1260 |
doc("Check that connections are shut down gracefully when stopping a listener."), |
| 1261 |
TransOpts = #{ |
| 1262 |
socket_opts => [{port, 0}], |
| 1263 |
shutdown => 1000 %% Shorter timeout to make the test case faster. |
| 1264 |
}, |
| 1265 |
Dispatch = cowboy_router:compile([{"localhost", [ |
| 1266 |
{"/delay_hello", delay_hello_h, |
| 1267 |
#{delay => 500, notify_received => self()}}, |
| 1268 |
{"/long_delay_hello", delay_hello_h, |
| 1269 |
#{delay => 10000, notify_received => self()}} |
| 1270 |
]}]), |
| 1271 |
ProtoOpts = #{ |
| 1272 |
env => #{dispatch => Dispatch} |
| 1273 |
}, |
| 1274 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, TransOpts, ProtoOpts), |
| 1275 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 1276 |
ConnPid1 = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 1277 |
Ref1 = gun:get(ConnPid1, "/delay_hello"), |
| 1278 |
ConnPid2 = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]), |
| 1279 |
Ref2 = gun:get(ConnPid2, "/long_delay_hello"), |
| 1280 |
%% Shutdown listener while the handlers are working. |
| 1281 |
receive {request_received, <<"/delay_hello">>} -> ok end, |
| 1282 |
receive {request_received, <<"/long_delay_hello">>} -> ok end, |
| 1283 |
%% Note: This call does not complete quickly and will |
| 1284 |
%% prevent other cowboy:stop_listener/1 calls to complete. |
| 1285 |
ok = cowboy:stop_listener(?FUNCTION_NAME), |
| 1286 |
%% Check that the 1st request is handled before shutting down. |
| 1287 |
{response, nofin, 200, RespHeaders} = gun:await(ConnPid1, Ref1), |
| 1288 |
<<"close">> = proplists:get_value(<<"connection">>, RespHeaders), |
| 1289 |
{ok, RespBody} = gun:await_body(ConnPid1, Ref1), |
| 1290 |
<<"Hello world!">> = iolist_to_binary(RespBody), |
| 1291 |
gun:close(ConnPid1), |
| 1292 |
%% Check that the 2nd (very slow) request is not handled. |
| 1293 |
{error, {stream_error, closed}} = gun:await(ConnPid2, Ref2), |
| 1294 |
gun:close(ConnPid2). |
| 1295 |
|
| 1296 |
send_timeout_close(_Config) -> |
| 1297 |
doc("Check that connections are closed on send timeout."), |
| 1298 |
TransOpts = #{ |
| 1299 |
socket_opts => [ |
| 1300 |
{port, 0}, |
| 1301 |
{send_timeout, 100}, |
| 1302 |
{send_timeout_close, true}, |
| 1303 |
{sndbuf, 10} |
| 1304 |
] |
| 1305 |
}, |
| 1306 |
Dispatch = cowboy_router:compile([{"localhost", [ |
| 1307 |
{"/endless", loop_handler_endless_h, #{delay => 100}} |
| 1308 |
]}]), |
| 1309 |
ProtoOpts = #{ |
| 1310 |
env => #{dispatch => Dispatch}, |
| 1311 |
idle_timeout => infinity |
| 1312 |
}, |
| 1313 |
{ok, _} = cowboy:start_clear(?FUNCTION_NAME, TransOpts, ProtoOpts), |
| 1314 |
Port = ranch:get_port(?FUNCTION_NAME), |
| 1315 |
try |
| 1316 |
%% Connect a client that sends a request and waits indefinitely. |
| 1317 |
{ok, ClientSocket} = gen_tcp:connect("localhost", Port, |
| 1318 |
[{recbuf, 10}, {buffer, 10}, {active, false}, {packet, 0}]), |
| 1319 |
ok = gen_tcp:send(ClientSocket, [ |
| 1320 |
"GET /endless HTTP/1.1\r\n", |
| 1321 |
"Host: localhost:", integer_to_list(Port), "\r\n", |
| 1322 |
"x-test-pid: ", pid_to_list(self()), "\r\n\r\n" |
| 1323 |
]), |
| 1324 |
%% Wait for the handler to start then get its pid, |
| 1325 |
%% the remote connection's pid and socket. |
| 1326 |
StreamPid = receive |
| 1327 |
{Self, StreamPid0, init} when Self =:= self() -> |
| 1328 |
StreamPid0 |
| 1329 |
after 1000 -> |
| 1330 |
error(timeout) |
| 1331 |
end, |
| 1332 |
ServerPid = ct_helper:get_remote_pid_tcp(ClientSocket), |
| 1333 |
{links, ServerLinks} = process_info(ServerPid, links), |
| 1334 |
[ServerSocket] = [PidOrPort || PidOrPort <- ServerLinks, is_port(PidOrPort)], |
| 1335 |
%% Poll the socket repeatedly until it is closed by the server. |
| 1336 |
WaitClosedFun = |
| 1337 |
fun F(T) when T =< 0 -> |
| 1338 |
error({status, prim_inet:getstatus(ServerSocket)}); |
| 1339 |
F(T) -> |
| 1340 |
Snooze = 100, |
| 1341 |
case inet:sockname(ServerSocket) of |
| 1342 |
{error, _} -> |
| 1343 |
timer:sleep(Snooze); |
| 1344 |
{ok, _} -> |
| 1345 |
timer:sleep(Snooze), |
| 1346 |
F(T - Snooze) |
| 1347 |
end |
| 1348 |
end, |
| 1349 |
ok = WaitClosedFun(2000), |
| 1350 |
false = erlang:is_process_alive(StreamPid), |
| 1351 |
false = erlang:is_process_alive(ServerPid) |
| 1352 |
after |
| 1353 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 1354 |
end. |