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

Initial HTTP/3 implementation

This includes Websocket over HTTP/3.

Since quicer, which provides the QUIC implementation,
is a NIF, Cowboy cannot depend directly on it. In order
to enable QUIC and HTTP/3, users have to set the
COWBOY_QUICER environment variable:

  export COWBOY_QUICER=1

In order to run the test suites, the same must be done
for Gun:

  export GUN_QUICER=1

HTTP/3 support is currently not available on Windows
due to compilation issues of quicer which have yet to
be looked at or resolved.

HTTP/3 support is also unavailable on the upcoming
OTP-27 due to compilation errors in quicer dependencies.
Once resolved HTTP/3 should work on OTP-27.

Because of how QUIC currently works, it's possible
that streams that get reset after sending a response
do not receive that response. The test suite was
modified to accomodate for that. A future extension
to QUIC will allow us to gracefully reset streams.

This also updates Erlang.mk.
This commit is contained in:
Loïc Hoguin 2023-01-31 11:07:31 +01:00
parent 3ea8395eb8
commit 8cb9d242b0
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
39 changed files with 5130 additions and 238 deletions

View file

@ -44,6 +44,8 @@ init_per_group(Name = h2, Config) ->
init_per_group(Name = h2c, Config) ->
Config1 = cowboy_test:init_http(Name, init_plain_opts(Config), Config),
lists:keyreplace(protocol, 1, Config1, {protocol, http2});
init_per_group(Name = h3, Config) ->
cowboy_test:init_http3(Name, init_plain_opts(Config), Config);
init_per_group(Name = http_compress, Config) ->
cowboy_test:init_http(Name, init_compress_opts(Config), Config);
init_per_group(Name = https_compress, Config) ->
@ -52,10 +54,12 @@ init_per_group(Name = h2_compress, Config) ->
cowboy_test:init_http2(Name, init_compress_opts(Config), Config);
init_per_group(Name = h2c_compress, Config) ->
Config1 = cowboy_test:init_http(Name, init_compress_opts(Config), Config),
lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
lists:keyreplace(protocol, 1, Config1, {protocol, http2});
init_per_group(Name = h3_compress, Config) ->
cowboy_test:init_http3(Name, init_compress_opts(Config), Config).
end_per_group(Name, _) ->
cowboy:stop_listener(Name).
cowboy_test:stop_group(Name).
init_plain_opts(Config) ->
#{
@ -157,16 +161,24 @@ do_get(Path, UserData, Config) ->
#{
ref := _,
pid := From,
streamid := 1,
reason := normal,
streamid := StreamID,
reason := normal, %% @todo Getting h3_no_error here.
req := #{},
informational := [],
user_data := UserData
} = Metrics,
do_check_streamid(StreamID, Config),
%% All good!
gun:close(ConnPid)
end.
do_check_streamid(StreamID, Config) ->
case config(protocol, Config) of
http -> 1 = StreamID;
http2 -> 1 = StreamID;
http3 -> 0 = StreamID
end.
post_body(Config) ->
doc("Confirm metrics are correct for a normal POST request."),
%% Perform a POST request.
@ -218,12 +230,13 @@ post_body(Config) ->
#{
ref := _,
pid := From,
streamid := 1,
streamid := StreamID,
reason := normal,
req := #{},
informational := [],
user_data := #{}
} = Metrics,
do_check_streamid(StreamID, Config),
%% All good!
gun:close(ConnPid)
end.
@ -273,12 +286,13 @@ no_resp_body(Config) ->
#{
ref := _,
pid := From,
streamid := 1,
streamid := StreamID,
reason := normal,
req := #{},
informational := [],
user_data := #{}
} = Metrics,
do_check_streamid(StreamID, Config),
%% All good!
gun:close(ConnPid)
end.
@ -291,7 +305,8 @@ early_error(Config) ->
%% reason in both protocols.
{Method, Headers, Status, Error} = case config(protocol, Config) of
http -> {<<"GET">>, [{<<"host">>, <<"host:port">>}], 400, protocol_error};
http2 -> {<<"TRACE">>, [], 501, no_error}
http2 -> {<<"TRACE">>, [], 501, no_error};
http3 -> {<<"TRACE">>, [], 501, h3_no_error}
end,
Ref = gun:request(ConnPid, Method, "/", [
{<<"accept-encoding">>, <<"gzip">>},
@ -305,7 +320,7 @@ early_error(Config) ->
#{
ref := _,
pid := From,
streamid := 1,
streamid := StreamID,
reason := {stream_error, Error, _},
partial_req := #{},
resp_status := Status,
@ -313,6 +328,7 @@ early_error(Config) ->
early_error_time := _,
resp_body_length := 0
} = Metrics,
do_check_streamid(StreamID, Config),
ExpectedRespHeaders = maps:from_list(RespHeaders),
%% All good!
gun:close(ConnPid)
@ -321,7 +337,8 @@ early_error(Config) ->
early_error_request_line(Config) ->
case config(protocol, Config) of
http -> do_early_error_request_line(Config);
http2 -> doc("There are no request lines in HTTP/2.")
http2 -> doc("There are no request lines in HTTP/2.");
http3 -> doc("There are no request lines in HTTP/3.")
end.
do_early_error_request_line(Config) ->
@ -341,7 +358,7 @@ do_early_error_request_line(Config) ->
#{
ref := _,
pid := From,
streamid := 1,
streamid := StreamID,
reason := {connection_error, protocol_error, _},
partial_req := #{},
resp_status := 400,
@ -349,6 +366,7 @@ do_early_error_request_line(Config) ->
early_error_time := _,
resp_body_length := 0
} = Metrics,
do_check_streamid(StreamID, Config),
ExpectedRespHeaders = maps:from_list(RespHeaders),
%% All good!
ok
@ -362,7 +380,9 @@ stream_reply(Config) ->
ws(Config) ->
case config(protocol, Config) of
http -> do_ws(Config);
http2 -> doc("It is not currently possible to switch to Websocket over HTTP/2.")
%% @todo The test can be implemented for HTTP/2.
http2 -> doc("It is not currently possible to switch to Websocket over HTTP/2.");
http3 -> {skip, "Gun does not currently support Websocket over HTTP/3."}
end.
do_ws(Config) ->
@ -405,7 +425,7 @@ do_ws(Config) ->
#{
ref := _,
pid := From,
streamid := 1,
streamid := StreamID,
reason := switch_protocol,
req := #{},
%% A 101 upgrade response was sent.
@ -420,6 +440,7 @@ do_ws(Config) ->
}],
user_data := #{}
} = Metrics,
do_check_streamid(StreamID, Config),
%% All good!
ok
end,
@ -438,7 +459,15 @@ error_response(Config) ->
{<<"accept-encoding">>, <<"gzip">>},
{<<"x-test-pid">>, pid_to_list(self())}
]),
{response, fin, 500, RespHeaders} = gun:await(ConnPid, Ref, infinity),
Protocol = config(protocol, Config),
RespHeaders = case gun:await(ConnPid, Ref, infinity) of
{response, fin, 500, RespHeaders0} ->
RespHeaders0;
%% The RST_STREAM arrived before the start of the response.
%% See maybe_h3_error comment for details.
{error, {stream_error, {stream_error, h3_internal_error, _}}} when Protocol =:= http3 ->
unknown
end,
timer:sleep(100),
%% Receive the metrics and validate them.
receive
@ -463,7 +492,14 @@ error_response(Config) ->
resp_headers := ExpectedRespHeaders,
resp_body_length := 0
} = Metrics,
ExpectedRespHeaders = maps:from_list(RespHeaders),
case RespHeaders of
%% The HTTP/3 stream has reset too early so we can't
%% verify the response headers.
unknown ->
ok;
_ ->
ExpectedRespHeaders = maps:from_list(RespHeaders)
end,
%% The request process executed normally.
#{procs := Procs} = Metrics,
[{_, #{
@ -476,12 +512,13 @@ error_response(Config) ->
#{
ref := _,
pid := From,
streamid := 1,
streamid := StreamID,
reason := {internal_error, {'EXIT', _Pid, {crash, StackTrace}}, 'Stream process crashed.'},
req := #{},
informational := [],
user_data := #{}
} = Metrics,
do_check_streamid(StreamID, Config),
%% All good!
gun:close(ConnPid)
end.
@ -495,7 +532,15 @@ error_response_after_reply(Config) ->
{<<"accept-encoding">>, <<"gzip">>},
{<<"x-test-pid">>, pid_to_list(self())}
]),
{response, fin, 200, RespHeaders} = gun:await(ConnPid, Ref, infinity),
Protocol = config(protocol, Config),
RespHeaders = case gun:await(ConnPid, Ref, infinity) of
{response, fin, 200, RespHeaders0} ->
RespHeaders0;
%% The RST_STREAM arrived before the start of the response.
%% See maybe_h3_error comment for details.
{error, {stream_error, {stream_error, h3_internal_error, _}}} when Protocol =:= http3 ->
unknown
end,
timer:sleep(100),
%% Receive the metrics and validate them.
receive
@ -520,7 +565,14 @@ error_response_after_reply(Config) ->
resp_headers := ExpectedRespHeaders,
resp_body_length := 0
} = Metrics,
ExpectedRespHeaders = maps:from_list(RespHeaders),
case RespHeaders of
%% The HTTP/3 stream has reset too early so we can't
%% verify the response headers.
unknown ->
ok;
_ ->
ExpectedRespHeaders = maps:from_list(RespHeaders)
end,
%% The request process executed normally.
#{procs := Procs} = Metrics,
[{_, #{
@ -533,12 +585,13 @@ error_response_after_reply(Config) ->
#{
ref := _,
pid := From,
streamid := 1,
streamid := StreamID,
reason := {internal_error, {'EXIT', _Pid, {crash, StackTrace}}, 'Stream process crashed.'},
req := #{},
informational := [],
user_data := #{}
} = Metrics,
do_check_streamid(StreamID, Config),
%% All good!
gun:close(ConnPid)
end.