diff options
author | Loïc Hoguin <[email protected]> | 2018-04-29 11:29:26 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2018-04-29 17:39:48 +0200 |
commit | 84b4128d068cddc37bbb45e755044183f3e9cd97 (patch) | |
tree | 641891c28f3409f92dafad2a7ea114c290ca423a /test/rfc7540_SUITE.erl | |
parent | 2db5ffbf842e0aeb0ae11484b8c247bbfdcf5e9e (diff) | |
download | cowboy-84b4128d068cddc37bbb45e755044183f3e9cd97.tar.gz cowboy-84b4128d068cddc37bbb45e755044183f3e9cd97.tar.bz2 cowboy-84b4128d068cddc37bbb45e755044183f3e9cd97.zip |
Receive and ignore HTTP/2 request trailers if any
This is a first step toward properly supporting request trailers.
Diffstat (limited to 'test/rfc7540_SUITE.erl')
-rw-r--r-- | test/rfc7540_SUITE.erl | 145 |
1 files changed, 133 insertions, 12 deletions
diff --git a/test/rfc7540_SUITE.erl b/test/rfc7540_SUITE.erl index 85e83a5..3d8bac4 100644 --- a/test/rfc7540_SUITE.erl +++ b/test/rfc7540_SUITE.erl @@ -3173,6 +3173,114 @@ settings_initial_window_size_reject_overflow(Config) -> % behavior. These MAY be treated by an implementation as being % equivalent to INTERNAL_ERROR. +accept_trailers(Config) -> + doc("Trailing HEADERS frames must be accepted. (RFC7540 8.1)"), + {ok, Socket} = do_handshake(Config), + %% Send a request containing DATA and trailing HEADERS frames. + {HeadersBlock, EncodeState} = cow_hpack:encode([ + {<<":method">>, <<"POST">>}, + {<<":scheme">>, <<"http">>}, + {<<":authority">>, <<"localhost">>}, %% @todo Correct port number. + {<<":path">>, <<"/long_polling">>}, + {<<"trailer">>, <<"x-checksum">>} + ]), + {TrailersBlock, _} = cow_hpack:encode([ + {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>} + ], EncodeState), + ok = gen_tcp:send(Socket, [ + cow_http2:headers(1, nofin, HeadersBlock), + cow_http2:data(1, nofin, <<0:10000/unit:8>>), + cow_http2:headers(1, fin, TrailersBlock) + ]), + %% Receive a HEADERS frame as a response. + {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000), + ok. + +accept_trailers_continuation(Config) -> + doc("Trailing HEADERS and CONTINUATION frames must be accepted. (RFC7540 8.1)"), + {ok, Socket} = do_handshake(Config), + %% Send a request containing DATA and trailing HEADERS and CONTINUATION frames. + {HeadersBlock, EncodeState} = cow_hpack:encode([ + {<<":method">>, <<"POST">>}, + {<<":scheme">>, <<"http">>}, + {<<":authority">>, <<"localhost">>}, %% @todo Correct port number. + {<<":path">>, <<"/long_polling">>}, + {<<"trailer">>, <<"x-checksum">>} + ]), + {TrailersBlock, _} = cow_hpack:encode([ + {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>} + ], EncodeState), + Len = iolist_size(TrailersBlock), + ok = gen_tcp:send(Socket, [ + cow_http2:headers(1, nofin, HeadersBlock), + cow_http2:data(1, nofin, <<0:10000/unit:8>>), + <<0:24, 1:8, 0:7, 1:1, 0:1, 1:31>>, + <<Len:24, 9:8, 0:5, 1:1, 0:3, 1:31>>, + TrailersBlock + ]), + %% Receive a HEADERS frame as a response. + {ok, << _:24, 1:8, _:40 >>} = gen_tcp:recv(Socket, 9, 6000), + ok. + +%% We reject all invalid HEADERS with a connection error because +%% we do not want to waste resources decoding them. +reject_trailers_nofin(Config) -> + doc("Trailing HEADERS frames received without the END_STREAM flag " + "set must be rejected with a PROTOCOL_ERROR connection error. " + "(RFC7540 8.1, RFC7540 8.1.2.6)"), + {ok, Socket} = do_handshake(Config), + %% Send a request containing DATA and trailing HEADERS frames. + %% The trailing HEADERS does not have the END_STREAM flag set. + {HeadersBlock, EncodeState} = cow_hpack:encode([ + {<<":method">>, <<"POST">>}, + {<<":scheme">>, <<"http">>}, + {<<":authority">>, <<"localhost">>}, %% @todo Correct port number. + {<<":path">>, <<"/long_polling">>}, + {<<"trailer">>, <<"x-checksum">>} + ]), + {TrailersBlock, _} = cow_hpack:encode([ + {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>} + ], EncodeState), + ok = gen_tcp:send(Socket, [ + cow_http2:headers(1, nofin, HeadersBlock), + cow_http2:data(1, nofin, <<0:10000/unit:8>>), + cow_http2:headers(1, nofin, TrailersBlock) + ]), + %% Receive a PROTOCOL_ERROR connection error. + {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000), + ok. + +%% We reject all invalid HEADERS with a connection error because +%% we do not want to waste resources decoding them. +reject_trailers_nofin_continuation(Config) -> + doc("Trailing HEADERS frames received without the END_STREAM flag " + "set must be rejected with a PROTOCOL_ERROR connection error. " + "(RFC7540 8.1, RFC7540 8.1.2.6)"), + {ok, Socket} = do_handshake(Config), + %% Send a request containing DATA and trailing HEADERS and CONTINUATION frames. + %% The trailing HEADERS does not have the END_STREAM flag set. + {HeadersBlock, EncodeState} = cow_hpack:encode([ + {<<":method">>, <<"POST">>}, + {<<":scheme">>, <<"http">>}, + {<<":authority">>, <<"localhost">>}, %% @todo Correct port number. + {<<":path">>, <<"/long_polling">>}, + {<<"trailer">>, <<"x-checksum">>} + ]), + {TrailersBlock, _} = cow_hpack:encode([ + {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>} + ], EncodeState), + Len = iolist_size(TrailersBlock), + ok = gen_tcp:send(Socket, [ + cow_http2:headers(1, nofin, HeadersBlock), + cow_http2:data(1, nofin, <<0:10000/unit:8>>), + <<0:24, 1:8, 0:9, 1:31>>, + <<Len:24, 9:8, 0:5, 1:1, 0:3, 1:31>>, + TrailersBlock + ]), + %% Receive a PROTOCOL_ERROR connection error. + {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000), + ok. + headers_informational_nofin(Config) -> doc("Informational HEADERS frames must not have the END_STREAM flag set. (RFC7540 8.1)"), {ok, Socket} = do_handshake(Config), @@ -3194,13 +3302,6 @@ headers_informational_nofin(Config) -> {_, <<"100">>} = lists:keyfind(<<":status">>, 1, RespHeaders), ok. -%% (RFC7540 8.1) -% A HEADERS frame (and associated CONTINUATION frames) can only appear -% at the start or end of a stream. An endpoint that receives a HEADERS -% frame without the END_STREAM flag set after receiving a final (non- -% informational) status code MUST treat the corresponding request or -% response as malformed (Section 8.1.2.6). -% %% @todo This one is interesting to implement because Cowboy DOES this. % A server can % send a complete response prior to the client sending an entire @@ -3261,11 +3362,31 @@ reject_unknown_pseudo_headers(Config) -> {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000), ok. -%% @todo Implement request trailers. reject_pseudo_headers_in_trailers(Config) -> -% Pseudo-header fields MUST NOT appear in trailers. -% Endpoints MUST treat a request or response that contains -% undefined or invalid pseudo-header fields as malformed -% (Section 8.1.2.6). +reject_pseudo_headers_in_trailers(Config) -> + doc("Requests containing pseudo-headers in trailers must be rejected " + "with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.1, RFC7540 8.1.2.6)"), + {ok, Socket} = do_handshake(Config), + %% Send a request containing DATA and trailing HEADERS frames. + %% The trailing HEADERS contains pseudo-headers. + {HeadersBlock, EncodeState} = cow_hpack:encode([ + {<<":method">>, <<"POST">>}, + {<<":scheme">>, <<"http">>}, + {<<":authority">>, <<"localhost">>}, %% @todo Correct port number. + {<<":path">>, <<"/long_polling">>}, + {<<"trailer">>, <<"x-checksum">>} + ]), + {TrailersBlock, _} = cow_hpack:encode([ + {<<"x-checksum">>, <<"md5:4cc909a007407f3706399b6496babec3">>}, + {<<":path">>, <<"/">>} + ], EncodeState), + ok = gen_tcp:send(Socket, [ + cow_http2:headers(1, nofin, HeadersBlock), + cow_http2:data(1, nofin, <<0:10000/unit:8>>), + cow_http2:headers(1, fin, TrailersBlock) + ]), + %% Receive a PROTOCOL_ERROR stream error. + {ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000), + ok. reject_pseudo_headers_after_regular_headers(Config) -> doc("Requests containing pseudo-headers after regular headers must be rejected " |