chore: allow handle */* or multiple values in Accept headers
This commit is contained in:
parent
1a79e34510
commit
966b2affc2
|
@ -2,7 +2,7 @@
|
||||||
{application, emqx_management, [
|
{application, emqx_management, [
|
||||||
{description, "EMQX Management API and CLI"},
|
{description, "EMQX Management API and CLI"},
|
||||||
% strict semver, bump manually!
|
% strict semver, bump manually!
|
||||||
{vsn, "5.0.25"},
|
{vsn, "5.0.26"},
|
||||||
{modules, []},
|
{modules, []},
|
||||||
{registered, [emqx_management_sup]},
|
{registered, [emqx_management_sup]},
|
||||||
{applications, [kernel, stdlib, emqx_plugins, minirest, emqx, emqx_ctl]},
|
{applications, [kernel, stdlib, emqx_plugins, minirest, emqx, emqx_ctl]},
|
||||||
|
|
|
@ -337,9 +337,10 @@ config_reset(post, _Params, Req) ->
|
||||||
|
|
||||||
configs(get, #{query_string := QueryStr, headers := Headers}, _Req) ->
|
configs(get, #{query_string := QueryStr, headers := Headers}, _Req) ->
|
||||||
%% Should deprecated json v1 since 5.2.0
|
%% Should deprecated json v1 since 5.2.0
|
||||||
case maps:get(<<"accept">>, Headers, <<"text/plain">>) of
|
case find_suitable_accept(Headers, [<<"text/plain">>, <<"application/json">>]) of
|
||||||
<<"application/json">> -> get_configs_v1(QueryStr);
|
{ok, <<"application/json">>} -> get_configs_v1(QueryStr);
|
||||||
<<"text/plain">> -> get_configs_v2(QueryStr)
|
{ok, <<"text/plain">>} -> get_configs_v2(QueryStr);
|
||||||
|
{error, _} = Error -> {400, #{code => 'INVALID_ACCEPT', message => ?ERR_MSG(Error)}}
|
||||||
end;
|
end;
|
||||||
configs(put, #{body := Conf, query_string := #{<<"mode">> := Mode}}, _Req) ->
|
configs(put, #{body := Conf, query_string := #{<<"mode">> := Mode}}, _Req) ->
|
||||||
case emqx_conf_cli:load_config(Conf, Mode) of
|
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)}}
|
{error, Errors} -> {400, #{code => 'UPDATE_FAILED', message => ?ERR_MSG(Errors)}}
|
||||||
end.
|
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) ->
|
get_configs_v1(QueryStr) ->
|
||||||
Node = maps:get(<<"node">>, QueryStr, node()),
|
Node = maps:get(<<"node">>, QueryStr, node()),
|
||||||
case
|
case
|
||||||
|
|
Loading…
Reference in New Issue