Make http heades passed in custom_headers override builtin headers

Previously we just appended them, so sometimes we just had duplicates

This fixes issue #3056
This commit is contained in:
Paweł Chmielowski 2020-02-21 12:19:02 +01:00
parent 3947e64524
commit 357e7e117d
3 changed files with 22 additions and 14 deletions

View File

@ -31,7 +31,8 @@
%% External exports %% External exports
-export([start/3, start_link/3, -export([start/3, start_link/3,
accept/1, receive_headers/1, recv_file/2, accept/1, receive_headers/1, recv_file/2,
listen_opt_type/1, listen_options/0]). listen_opt_type/1, listen_options/0,
apply_custom_headers/2]).
-export([init/3]). -export([init/3]).
@ -491,19 +492,19 @@ process_request(#state{request_method = Method,
{Status, Headers, El} {Status, Headers, El}
when is_record(El, xmlel) -> when is_record(El, xmlel) ->
make_xhtml_output(State, Status, make_xhtml_output(State, Status,
Headers ++ CustomHeaders, El); apply_custom_headers(Headers, CustomHeaders), El);
Output when is_binary(Output) or is_list(Output) -> Output when is_binary(Output) or is_list(Output) ->
make_text_output(State, 200, CustomHeaders, Output); make_text_output(State, 200, CustomHeaders, Output);
{Status, Headers, Output} {Status, Headers, Output}
when is_binary(Output) or is_list(Output) -> when is_binary(Output) or is_list(Output) ->
make_text_output(State, Status, make_text_output(State, Status,
Headers ++ CustomHeaders, Output); apply_custom_headers(Headers, CustomHeaders), Output);
{Status, Headers, {file, FileName}} -> {Status, Headers, {file, FileName}} ->
make_file_output(State, Status, Headers, FileName); make_file_output(State, Status, Headers, FileName);
{Status, Reason, Headers, Output} {Status, Reason, Headers, Output}
when is_binary(Output) or is_list(Output) -> when is_binary(Output) or is_list(Output) ->
make_text_output(State, Status, Reason, make_text_output(State, Status, Reason,
Headers ++ CustomHeaders, Output); apply_custom_headers(Headers, CustomHeaders), Output);
_ -> _ ->
none none
end, end,
@ -855,6 +856,11 @@ parse_urlencoded(<<>>, Last, Cur, _State) ->
[{Last, Cur}]; [{Last, Cur}];
parse_urlencoded(undefined, _, _, _) -> []. parse_urlencoded(undefined, _, _, _) -> [].
apply_custom_headers(Headers, CustomHeaders) ->
M = maps:merge(maps:from_list(Headers),
maps:from_list(CustomHeaders)),
maps:to_list(M).
% The following code is mostly taken from yaws_ssl.erl % The following code is mostly taken from yaws_ssl.erl
toupper(C) when C >= $a andalso C =< $z -> C - 32; toupper(C) when C >= $a andalso C =< $z -> C - 32;

View File

@ -345,9 +345,10 @@ serve_index(FileName, [Index | T], CH, DefaultContentType, ContentTypes) ->
serve_not_modified(FileInfo, FileName, CustomHeaders) -> serve_not_modified(FileInfo, FileName, CustomHeaders) ->
?DEBUG("Delivering not modified: ~ts", [FileName]), ?DEBUG("Delivering not modified: ~ts", [FileName]),
{0, 304, {0, 304,
[{<<"Server">>, <<"ejabberd">>}, ejabberd_http:apply_custom_headers(
{<<"Last-Modified">>, last_modified(FileInfo)} [{<<"Server">>, <<"ejabberd">>},
| CustomHeaders], <<>>}. {<<"Last-Modified">>, last_modified(FileInfo)}],
CustomHeaders), <<>>}.
%% Assume the file exists if we got this far and attempt to read it in %% Assume the file exists if we got this far and attempt to read it in
%% and serve it up. %% and serve it up.
@ -356,10 +357,11 @@ serve_file(FileInfo, FileName, CustomHeaders, DefaultContentType, ContentTypes)
ContentType = content_type(FileName, DefaultContentType, ContentType = content_type(FileName, DefaultContentType,
ContentTypes), ContentTypes),
{FileInfo#file_info.size, 200, {FileInfo#file_info.size, 200,
[{<<"Server">>, <<"ejabberd">>}, ejabberd_http:apply_custom_headers(
{<<"Last-Modified">>, last_modified(FileInfo)}, [{<<"Server">>, <<"ejabberd">>},
{<<"Content-Type">>, ContentType} {<<"Last-Modified">>, last_modified(FileInfo)},
| CustomHeaders], {<<"Content-Type">>, ContentType}],
CustomHeaders),
{file, FileName}}. {file, FileName}}.
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------

View File

@ -545,7 +545,7 @@ process(_LocalPath, #request{method = 'PUT', host = Host, ip = IP,
ok -> ok ->
http_response(201, CustomHeaders); http_response(201, CustomHeaders);
{ok, Headers, OutData} -> {ok, Headers, OutData} ->
http_response(201, Headers ++ CustomHeaders, OutData); http_response(201, ejabberd_http:apply_custom_headers(Headers, CustomHeaders), OutData);
{error, closed} -> {error, closed} ->
?DEBUG("Cannot store file ~ts from ~ts for ~ts: connection closed", ?DEBUG("Cannot store file ~ts from ~ts for ~ts: connection closed",
[Path, encode_addr(IP), Host]), [Path, encode_addr(IP), Host]),
@ -595,7 +595,7 @@ process(_LocalPath, #request{method = Method, host = Host, ip = IP} = Request)
$", FileName/binary, $">>}] $", FileName/binary, $">>}]
end, end,
Headers2 = [{<<"Content-Type">>, ContentType} | Headers1], Headers2 = [{<<"Content-Type">>, ContentType} | Headers1],
Headers3 = Headers2 ++ CustomHeaders, Headers3 = ejabberd_http:apply_custom_headers(Headers2, CustomHeaders),
http_response(200, Headers3, {file, Path}); http_response(200, Headers3, {file, Path});
{error, eacces} -> {error, eacces} ->
?WARNING_MSG("Cannot serve ~ts to ~ts: Permission denied", ?WARNING_MSG("Cannot serve ~ts to ~ts: Permission denied",
@ -633,7 +633,7 @@ process(_LocalPath, #request{method = 'OPTIONS', host = Host,
try gen_server:call(Proc, get_conf, ?CALL_TIMEOUT) of try gen_server:call(Proc, get_conf, ?CALL_TIMEOUT) of
{ok, _DocRoot, CustomHeaders} -> {ok, _DocRoot, CustomHeaders} ->
AllowHeader = {<<"Allow">>, <<"OPTIONS, HEAD, GET, PUT">>}, AllowHeader = {<<"Allow">>, <<"OPTIONS, HEAD, GET, PUT">>},
http_response(200, [AllowHeader | CustomHeaders]) http_response(200, ejabberd_http:apply_custom_headers([AllowHeader], CustomHeaders))
catch catch
exit:{noproc, _} -> exit:{noproc, _} ->
?WARNING_MSG("Cannot handle OPTIONS request from ~ts for ~ts: " ?WARNING_MSG("Cannot handle OPTIONS request from ~ts for ~ts: "