Browse Source

Use default port when not given in :authority

Loïc Hoguin 7 years ago
parent
commit
d732e74dd5
3 changed files with 13 additions and 2 deletions
  1. 6 1
      src/cowboy_http2.erl
  2. 1 1
      src/cowboy_req.erl
  3. 6 0
      test/req_SUITE.erl

+ 6 - 1
src/cowboy_http2.erl

@@ -1119,7 +1119,8 @@ stream_req_init(State=#state{ref=Ref, peer=Peer, sock=Sock, cert=Cert},
 		StreamID, IsFin, Headers, PseudoHeaders=#{method := Method, scheme := Scheme,
 		StreamID, IsFin, Headers, PseudoHeaders=#{method := Method, scheme := Scheme,
 			authority := Authority, path := PathWithQs}, BodyLength) ->
 			authority := Authority, path := PathWithQs}, BodyLength) ->
 	try cow_http_hd:parse_host(Authority) of
 	try cow_http_hd:parse_host(Authority) of
-		{Host, Port} ->
+		{Host, Port0} ->
+			Port = ensure_port(Scheme, Port0),
 			try cow_http:parse_fullpath(PathWithQs) of
 			try cow_http:parse_fullpath(PathWithQs) of
 				{<<>>, _} ->
 				{<<>>, _} ->
 					stream_malformed(State, StreamID,
 					stream_malformed(State, StreamID,
@@ -1160,6 +1161,10 @@ stream_req_init(State=#state{ref=Ref, peer=Peer, sock=Sock, cert=Cert},
 			'The :authority pseudo-header is invalid. (RFC7540 8.1.2.3)')
 			'The :authority pseudo-header is invalid. (RFC7540 8.1.2.3)')
 	end.
 	end.
 
 
+ensure_port(<<"http">>, undefined) -> 80;
+ensure_port(<<"https">>, undefined) -> 443;
+ensure_port(_, Port) -> Port.
+
 stream_closed(State=#state{socket=Socket, transport=Transport}, StreamID, _) ->
 stream_closed(State=#state{socket=Socket, transport=Transport}, StreamID, _) ->
 	Transport:send(Socket, cow_http2:rst_stream(StreamID, stream_closed)),
 	Transport:send(Socket, cow_http2:rst_stream(StreamID, stream_closed)),
 	State.
 	State.

+ 1 - 1
src/cowboy_req.erl

@@ -110,7 +110,7 @@
 	method => binary(),
 	method => binary(),
 	scheme => binary(),
 	scheme => binary(),
 	host => binary(),
 	host => binary(),
-	port => binary(),
+	port => inet:port_number(),
 	qs => binary()
 	qs => binary()
 }.
 }.
 -export_type([push_opts/0]).
 -export_type([push_opts/0]).

+ 6 - 0
test/req_SUITE.erl

@@ -334,6 +334,12 @@ port(Config) ->
 	Port = integer_to_binary(config(port, Config)),
 	Port = integer_to_binary(config(port, Config)),
 	Port = do_get_body("/port", Config),
 	Port = do_get_body("/port", Config),
 	Port = do_get_body("/direct/port", Config),
 	Port = do_get_body("/direct/port", Config),
+	ExpectedPort = case config(type, Config) of
+		tcp -> <<"80">>;
+		ssl -> <<"443">>
+	end,
+	ExpectedPort = do_get_body("/port", [{<<"host">>, <<"localhost">>}], Config),
+	ExpectedPort = do_get_body("/direct/port", [{<<"host">>, <<"localhost">>}], Config),
 	ok.
 	ok.
 
 
 qs(Config) ->
 qs(Config) ->