Skip to content

Commit

Permalink
Fallback to host header if authority is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
fredr committed Jan 24, 2019
1 parent e0254cd commit 059dfd3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/cowboy_http2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,14 @@ headers_frame(State, StreamID, IsFin, Headers,
'The TRACE method is currently not implemented. (RFC7231 4.3.8)');
headers_frame(State=#state{ref=Ref, peer=Peer, sock=Sock, cert=Cert, proxy_header=ProxyHeader},
StreamID, IsFin, Headers, PseudoHeaders=#{method := Method, scheme := Scheme,
authority := Authority, path := PathWithQs}, BodyLen) ->
path := PathWithQs}, BodyLen) ->
Authority = case PseudoHeaders of
#{authority := Authority0} -> Authority0;
_ -> case lists:keyfind(<<"host">>, 1, Headers) of
{<<"host">>, Host0} -> Host0;
_ -> undefined
end
end,
try cow_http_hd:parse_host(Authority) of
{Host, Port0} ->
Port = ensure_port(Scheme, Port0),
Expand Down
21 changes: 20 additions & 1 deletion test/rfc7540_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3746,7 +3746,7 @@ reject_many_pseudo_header_scheme(Config) ->
ok.

reject_missing_pseudo_header_authority(Config) ->
doc("A request without an authority component must be rejected "
doc("A request without an authority or host component must be rejected "
"with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
{ok, Socket} = do_handshake(Config),
%% Send a HEADERS frame without an :authority pseudo-header.
Expand All @@ -3760,6 +3760,25 @@ reject_missing_pseudo_header_authority(Config) ->
{ok, << _:24, 3:8, _:8, 1:32, 1:32 >>} = gen_tcp:recv(Socket, 13, 6000),
ok.

fallback_to_host_on_missing_pseudo_header_authority(Config) ->
doc("Fallback to use the host header if pseudo header :authority is missing"),
{ok, Socket} = do_handshake(Config),
%% Send a HEADERS frame with host header and without an :authority pseudo-header.
{HeadersBlock, _} = cow_hpack:encode([
{<<":method">>, <<"GET">>},
{<<":scheme">>, <<"http">>},
{<<":path">>, <<"/">>},
{<<"host">>, <<"localhost">>}
]),
ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
%% Receive an informational HEADERS frame
{ok, << Len:24, 1:8, _:8, _:32 >>} = gen_tcp:recv(Socket, 9, 6000),
{ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len, 6000),
%% Confirm it has a 200 status code.
{RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
{_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
ok.

reject_many_pseudo_header_authority(Config) ->
doc("A request containing more than one authority component must be rejected "
"with a PROTOCOL_ERROR stream error. (RFC7540 8.1.2.3, RFC7540 8.1.2.6)"),
Expand Down

0 comments on commit 059dfd3

Please sign in to comment.