emqttd_topic.erl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. %%%-----------------------------------------------------------------------------
  2. %%% Copyright (c) 2012-2015 eMQTT.IO, All Rights Reserved.
  3. %%%
  4. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  5. %%% of this software and associated documentation files (the "Software"), to deal
  6. %%% in the Software without restriction, including without limitation the rights
  7. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. %%% copies of the Software, and to permit persons to whom the Software is
  9. %%% furnished to do so, subject to the following conditions:
  10. %%%
  11. %%% The above copyright notice and this permission notice shall be included in all
  12. %%% copies or substantial portions of the Software.
  13. %%%
  14. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. %%% SOFTWARE.
  21. %%%-----------------------------------------------------------------------------
  22. %%% @doc
  23. %%% MQTT Topic
  24. %%%
  25. %%% @end
  26. %%%-----------------------------------------------------------------------------
  27. -module(emqttd_topic).
  28. -author("Feng Lee <feng@emqtt.io>").
  29. -import(lists, [reverse/1]).
  30. -export([match/2, validate/1, triples/1, words/1, wildcard/1]).
  31. -export([join/1, feed_var/3, is_queue/1, systop/1]).
  32. %-type type() :: static | dynamic.
  33. -type word() :: '' | '+' | '#' | binary().
  34. -type words() :: list(word()).
  35. -type triple() :: {root | binary(), word(), binary()}.
  36. -export_type([word/0, triple/0]).
  37. -define(MAX_TOPIC_LEN, 4096).
  38. %%%-----------------------------------------------------------------------------
  39. %% @doc Is wildcard topic?
  40. %% @end
  41. %%%-----------------------------------------------------------------------------
  42. -spec wildcard(binary()) -> true | false.
  43. wildcard(Topic) when is_binary(Topic) ->
  44. wildcard(words(Topic));
  45. wildcard([]) ->
  46. false;
  47. wildcard(['#'|_]) ->
  48. true;
  49. wildcard(['+'|_]) ->
  50. true;
  51. wildcard([_H|T]) ->
  52. wildcard(T).
  53. %%------------------------------------------------------------------------------
  54. %% @doc Match Topic name with filter
  55. %% @end
  56. %%------------------------------------------------------------------------------
  57. -spec match(Name, Filter) -> boolean() when
  58. Name :: binary() | words(),
  59. Filter :: binary() | words().
  60. match(Name, Filter) when is_binary(Name) and is_binary(Filter) ->
  61. match(words(Name), words(Filter));
  62. match([], []) ->
  63. true;
  64. match([H|T1], [H|T2]) ->
  65. match(T1, T2);
  66. match([<<$$, _/binary>>|_], ['+'|_]) ->
  67. false;
  68. match([_H|T1], ['+'|T2]) ->
  69. match(T1, T2);
  70. match([<<$$, _/binary>>|_], ['#']) ->
  71. false;
  72. match(_, ['#']) ->
  73. true;
  74. match([_H1|_], [_H2|_]) ->
  75. false;
  76. match([_H1|_], []) ->
  77. false;
  78. match([], [_H|_T2]) ->
  79. false.
  80. %%------------------------------------------------------------------------------
  81. %% @doc Validate Topic
  82. %% @end
  83. %%------------------------------------------------------------------------------
  84. -spec validate({name | filter, binary()}) -> boolean().
  85. validate({_, <<>>}) ->
  86. false;
  87. validate({_, Topic}) when is_binary(Topic) and (size(Topic) > ?MAX_TOPIC_LEN) ->
  88. false;
  89. validate({filter, Topic}) when is_binary(Topic) ->
  90. validate2(words(Topic));
  91. validate({name, Topic}) when is_binary(Topic) ->
  92. Words = words(Topic),
  93. validate2(Words) and (not wildcard(Words)).
  94. validate2([]) ->
  95. true;
  96. validate2(['#']) -> % end with '#'
  97. true;
  98. validate2(['#'|Words]) when length(Words) > 0 ->
  99. false;
  100. validate2([''|Words]) ->
  101. validate2(Words);
  102. validate2(['+'|Words]) ->
  103. validate2(Words);
  104. validate2([W|Words]) ->
  105. case validate3(W) of
  106. true -> validate2(Words);
  107. false -> false
  108. end.
  109. validate3(<<>>) ->
  110. true;
  111. validate3(<<C/utf8, _Rest/binary>>) when C == $#; C == $+; C == 0 ->
  112. false;
  113. validate3(<<_/utf8, Rest/binary>>) ->
  114. validate3(Rest).
  115. %%%-----------------------------------------------------------------------------
  116. %% @doc Topic to Triples
  117. %% @end
  118. %%%-----------------------------------------------------------------------------
  119. -spec triples(binary()) -> list(triple()).
  120. triples(Topic) when is_binary(Topic) ->
  121. triples(words(Topic), root, []).
  122. triples([], _Parent, Acc) ->
  123. reverse(Acc);
  124. triples([W|Words], Parent, Acc) ->
  125. Node = join(Parent, W),
  126. triples(Words, Node, [{Parent, W, Node}|Acc]).
  127. join(root, W) ->
  128. bin(W);
  129. join(Parent, W) ->
  130. <<(bin(Parent))/binary, $/, (bin(W))/binary>>.
  131. bin('') -> <<>>;
  132. bin('+') -> <<"+">>;
  133. bin('#') -> <<"#">>;
  134. bin(B) when is_binary(B) -> B.
  135. %%------------------------------------------------------------------------------
  136. %% @doc Split Topic Path to Words
  137. %% @end
  138. %%------------------------------------------------------------------------------
  139. -spec words(binary()) -> words().
  140. words(Topic) when is_binary(Topic) ->
  141. [word(W) || W <- binary:split(Topic, <<"/">>, [global])].
  142. word(<<>>) -> '';
  143. word(<<"+">>) -> '+';
  144. word(<<"#">>) -> '#';
  145. word(Bin) -> Bin.
  146. %%------------------------------------------------------------------------------
  147. %% @doc Queue is a special topic name that starts with "$Q/"
  148. %% @end
  149. %%------------------------------------------------------------------------------
  150. -spec is_queue(binary()) -> boolean().
  151. is_queue(<<"$Q/", _Queue/binary>>) ->
  152. true;
  153. is_queue(_) ->
  154. false.
  155. %%------------------------------------------------------------------------------
  156. %% @doc '$SYS' Topic.
  157. %% @end
  158. %%------------------------------------------------------------------------------
  159. systop(Name) when is_atom(Name) ->
  160. list_to_binary(lists:concat(["$SYS/brokers/", node(), "/", Name]));
  161. systop(Name) when is_binary(Name) ->
  162. list_to_binary(["$SYS/brokers/", atom_to_list(node()), "/", Name]).
  163. -spec feed_var(binary(), binary(), binary()) -> binary().
  164. feed_var(Var, Val, Topic) ->
  165. feed_var(Var, Val, words(Topic), []).
  166. feed_var(_Var, _Val, [], Acc) ->
  167. join(lists:reverse(Acc));
  168. feed_var(Var, Val, [Var|Words], Acc) ->
  169. feed_var(Var, Val, Words, [Val|Acc]);
  170. feed_var(Var, Val, [W|Words], Acc) ->
  171. feed_var(Var, Val, Words, [W|Acc]).
  172. -spec join(list(binary())) -> binary().
  173. join([]) ->
  174. <<>>;
  175. join([W]) ->
  176. bin(W);
  177. join(Words) ->
  178. {_, Bin} =
  179. lists:foldr(fun(W, {true, Tail}) ->
  180. {false, <<W/binary, Tail/binary>>};
  181. (W, {false, Tail}) ->
  182. {false, <<W/binary, "/", Tail/binary>>}
  183. end, {true, <<>>}, [bin(W) || W <- Words]),
  184. Bin.