From 83f5da8f9d39d801b5e216328578f759e43ad222 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Fri, 1 Jul 2022 16:46:13 +0800 Subject: [PATCH] fix(authz-http): fix https://github.com/emqx/emqx/pull/8377#discussion_r911743360 --- apps/emqx_authz/src/emqx_authz_http.erl | 15 ++++++++++----- apps/emqx_authz/src/emqx_authz_utils.erl | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/apps/emqx_authz/src/emqx_authz_http.erl b/apps/emqx_authz/src/emqx_authz_http.erl index 4579a3729..79e434587 100644 --- a/apps/emqx_authz/src/emqx_authz_http.erl +++ b/apps/emqx_authz/src/emqx_authz_http.erl @@ -85,11 +85,7 @@ authorize( {ok, 204, _Headers} -> {matched, allow}; {ok, 200, Headers, Body} -> - ContentType = proplists:get_value( - <<"content-type">>, - Headers, - <<"application/json">> - ), + ContentType = content_type(Headers), case emqx_authz_utils:parse_http_resp_body(ContentType, Body) of error -> ?SLOG(error, #{ @@ -222,6 +218,15 @@ serialize_body(<<"application/json">>, Body) -> serialize_body(<<"application/x-www-form-urlencoded">>, Body) -> query_string(Body). +content_type(Headers) when is_list(Headers) -> + content_type(maps:from_list(Headers)); +content_type(#{<<"content-type">> := Type}) -> + Type; +content_type(#{<<"Content-Type">> := Type}) -> + Type; +content_type(Headers) when is_map(Headers) -> + <<"application/json">>. + client_vars(Client, PubSub, Topic) -> Client#{ action => PubSub, diff --git a/apps/emqx_authz/src/emqx_authz_utils.erl b/apps/emqx_authz/src/emqx_authz_utils.erl index e8b3d1e34..5b91b7d57 100644 --- a/apps/emqx_authz/src/emqx_authz_utils.erl +++ b/apps/emqx_authz/src/emqx_authz_utils.erl @@ -133,13 +133,13 @@ render_sql_params(ParamList, Values) -> ). -spec parse_http_resp_body(binary(), binary()) -> allow | deny | ignore | error. -parse_http_resp_body(<<"application/x-www-form-urlencoded">>, Body) -> +parse_http_resp_body(<<"application/x-www-form-urlencoded", _/binary>>, Body) -> try result(maps:from_list(cow_qs:parse_qs(Body))) catch _:_ -> error end; -parse_http_resp_body(<<"application/json">>, Body) -> +parse_http_resp_body(<<"application/json", _/binary>>, Body) -> try result(emqx_json:decode(Body, [return_maps])) catch