Просмотр исходного кода

Don't send an unnecessary content-type header with cowboy_rest

Loïc Hoguin 6 лет назад
Родитель
Сommit
637a9b3924
2 измененных файлов с 11 добавлено и 4 удалено
  1. 9 1
      src/cowboy_rest.erl
  2. 2 3
      test/static_handler_SUITE.erl

+ 9 - 1
src/cowboy_rest.erl

@@ -1595,7 +1595,15 @@ next(Req, State, Next) when is_function(Next) ->
 next(Req, State, StatusCode) when is_integer(StatusCode) ->
 	respond(Req, State, StatusCode).
 
-respond(Req, State, StatusCode) ->
+respond(Req0, State, StatusCode) ->
+	%% We remove the content-type header when there is no body,
+	%% except when the status code is 200 because it might have
+	%% been intended (for example sending an empty file).
+	Req = case cowboy_req:has_resp_body(Req0) of
+		true when StatusCode =:= 200 -> Req0;
+		true -> Req0;
+		false -> cowboy_req:delete_resp_header(<<"content-type">>, Req0)
+	end,
 	terminate(cowboy_req:reply(StatusCode, Req), State).
 
 switch_handler({switch_handler, Mod}, Req, #state{handler_state=HandlerState}) ->

+ 2 - 3
test/static_handler_SUITE.erl

@@ -392,9 +392,8 @@ dir_error_directory_slash(Config) ->
 
 dir_error_doesnt_exist(Config) ->
 	doc("Try to get a file that does not exist."),
-	%% @todo Check that the content-type header is removed.
-	{404, _Headers, _} = do_get(config(prefix, Config) ++ "/not.found", Config),
-%	false = lists:keyfind(<<"content-type">>, 1, Headers),
+	{404, Headers, _} = do_get(config(prefix, Config) ++ "/not.found", Config),
+	false = lists:keyfind(<<"content-type">>, 1, Headers),
 	ok.
 
 dir_error_dot(Config) ->