chore: allow handle */* or multiple values in Accept headers

This commit is contained in:
JianBo He 2023-07-06 10:53:07 +08:00
parent 1a79e34510
commit 966b2affc2
2 changed files with 27 additions and 4 deletions

View File

@ -2,7 +2,7 @@
{application, emqx_management, [
{description, "EMQX Management API and CLI"},
% strict semver, bump manually!
{vsn, "5.0.25"},
{vsn, "5.0.26"},
{modules, []},
{registered, [emqx_management_sup]},
{applications, [kernel, stdlib, emqx_plugins, minirest, emqx, emqx_ctl]},

View File

@ -337,9 +337,10 @@ config_reset(post, _Params, Req) ->
configs(get, #{query_string := QueryStr, headers := Headers}, _Req) ->
%% Should deprecated json v1 since 5.2.0
case maps:get(<<"accept">>, Headers, <<"text/plain">>) of
<<"application/json">> -> get_configs_v1(QueryStr);
<<"text/plain">> -> get_configs_v2(QueryStr)
case find_suitable_accept(Headers, [<<"text/plain">>, <<"application/json">>]) of
{ok, <<"application/json">>} -> get_configs_v1(QueryStr);
{ok, <<"text/plain">>} -> get_configs_v2(QueryStr);
{error, _} = Error -> {400, #{code => 'INVALID_ACCEPT', message => ?ERR_MSG(Error)}}
end;
configs(put, #{body := Conf, query_string := #{<<"mode">> := Mode}}, _Req) ->
case emqx_conf_cli:load_config(Conf, Mode) of
@ -348,6 +349,28 @@ configs(put, #{body := Conf, query_string := #{<<"mode">> := Mode}}, _Req) ->
{error, Errors} -> {400, #{code => 'UPDATE_FAILED', message => ?ERR_MSG(Errors)}}
end.
find_suitable_accept(Headers, Perferences) ->
AcceptVal = maps:get(<<"accept">>, Headers, <<"*/*">>),
%% Multiple types, weighted with the quality value syntax:
%% Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8
Accepts = lists:map(
fun(S) ->
[T | _] = binary:split(string:trim(S), <<";">>),
T
end,
re:split(AcceptVal, ",")
),
case lists:member(<<"*/*">>, Accepts) of
true ->
{ok, lists:first(Perferences)};
fales ->
Found = lists:filter(fun(Accept) -> lists:member(Accept, Accepts) end, Perferences),
case Found of
[] -> {error, no_suitalbe_accept};
_ -> lists:first(Found)
end
end.
get_configs_v1(QueryStr) ->
Node = maps:get(<<"node">>, QueryStr, node()),
case