refactor: check match topic before do subscriptions query
This commit is contained in:
parent
e9de7316b6
commit
3a09fdc495
|
@ -142,21 +142,9 @@ parameters() ->
|
||||||
|
|
||||||
subscriptions(get, #{query_string := QString}) ->
|
subscriptions(get, #{query_string := QString}) ->
|
||||||
Response =
|
Response =
|
||||||
try
|
case check_match_topic(QString) of
|
||||||
begin
|
ok -> do_subscriptions_query(QString);
|
||||||
case maps:get(<<"match_topic">>, QString, undefined) of
|
{error, _} = Err -> Err
|
||||||
undefined ->
|
|
||||||
do_subscriptions_query(QString);
|
|
||||||
MatchTopic ->
|
|
||||||
case emqx_topic:parse(MatchTopic) of
|
|
||||||
{#share{}, _} -> {error, invalid_match_topic};
|
|
||||||
_ -> do_subscriptions_query(QString)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
catch
|
|
||||||
error:{invalid_topic_filter, _} ->
|
|
||||||
{error, invalid_match_topic}
|
|
||||||
end,
|
end,
|
||||||
case Response of
|
case Response of
|
||||||
{error, invalid_match_topic} ->
|
{error, invalid_match_topic} ->
|
||||||
|
@ -170,6 +158,31 @@ subscriptions(get, #{query_string := QString}) ->
|
||||||
{200, Result}
|
{200, Result}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
format(WhichNode, {{Topic, _Subscriber}, SubOpts}) ->
|
||||||
|
maps:merge(
|
||||||
|
#{
|
||||||
|
topic => emqx_topic:maybe_format_share(Topic),
|
||||||
|
clientid => maps:get(subid, SubOpts, null),
|
||||||
|
node => WhichNode
|
||||||
|
},
|
||||||
|
maps:with([qos, nl, rap, rh], SubOpts)
|
||||||
|
).
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% Internal functions
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
|
check_match_topic(#{<<"match_topic">> := MatchTopic}) ->
|
||||||
|
try emqx_topic:parse(MatchTopic) of
|
||||||
|
{#share{}, _} -> {error, invalid_match_topic};
|
||||||
|
_ -> ok
|
||||||
|
catch
|
||||||
|
error:{invalid_topic_filter, _} ->
|
||||||
|
{error, invalid_match_topic}
|
||||||
|
end;
|
||||||
|
check_match_topic(_) ->
|
||||||
|
ok.
|
||||||
|
|
||||||
do_subscriptions_query(QString) ->
|
do_subscriptions_query(QString) ->
|
||||||
Args = [?SUBOPTION, QString, ?SUBS_QSCHEMA, fun ?MODULE:qs2ms/2, fun ?MODULE:format/2],
|
Args = [?SUBOPTION, QString, ?SUBS_QSCHEMA, fun ?MODULE:qs2ms/2, fun ?MODULE:format/2],
|
||||||
case maps:get(<<"node">>, QString, undefined) of
|
case maps:get(<<"node">>, QString, undefined) of
|
||||||
|
@ -184,16 +197,6 @@ do_subscriptions_query(QString) ->
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
format(WhichNode, {{Topic, _Subscriber}, SubOpts}) ->
|
|
||||||
maps:merge(
|
|
||||||
#{
|
|
||||||
topic => emqx_topic:maybe_format_share(Topic),
|
|
||||||
clientid => maps:get(subid, SubOpts, null),
|
|
||||||
node => WhichNode
|
|
||||||
},
|
|
||||||
maps:with([qos, nl, rap, rh], SubOpts)
|
|
||||||
).
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% QueryString to MatchSpec
|
%% QueryString to MatchSpec
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue