list -> binary
This commit is contained in:
parent
5fc497ec1a
commit
20fb39cf3b
|
@ -18,7 +18,7 @@
|
||||||
%% Copyright (c) 2007-2012 VMware, Inc. All rights reserved.
|
%% Copyright (c) 2007-2012 VMware, Inc. All rights reserved.
|
||||||
%%
|
%%
|
||||||
|
|
||||||
-define(PROTOCOL_NAMES, [{3, "MQIsdp"}, {4, "MQTT"}]).
|
-define(PROTOCOL_NAMES, [{3, <<"MQIsdp">>}, {4, <<"MQTT">>}]).
|
||||||
|
|
||||||
-define(MQTT_PROTO_MAJOR, 3).
|
-define(MQTT_PROTO_MAJOR, 3).
|
||||||
-define(MQTT_PROTO_MINOR, 1).
|
-define(MQTT_PROTO_MINOR, 1).
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
|
|
||||||
-export([start_link/0,
|
-export([start_link/0,
|
||||||
add/2,
|
add/2,
|
||||||
check/2,
|
check/1, check/2,
|
||||||
delete/1]).
|
delete/1]).
|
||||||
|
|
||||||
-behavior(gen_server).
|
-behavior(gen_server).
|
||||||
|
@ -42,9 +42,15 @@
|
||||||
terminate/2,
|
terminate/2,
|
||||||
code_change/3]).
|
code_change/3]).
|
||||||
|
|
||||||
|
-define(TAB, ?MODULE).
|
||||||
|
|
||||||
start_link() ->
|
start_link() ->
|
||||||
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
||||||
|
|
||||||
|
-spec check({Usename :: binary(), Password :: binary()}) -> true | false.
|
||||||
|
check({Username, Password}) ->
|
||||||
|
execute(check, [Username, Password]).
|
||||||
|
|
||||||
-spec check(Usename :: binary(), Password :: binary()) -> true | false.
|
-spec check(Usename :: binary(), Password :: binary()) -> true | false.
|
||||||
check(Username, Password) ->
|
check(Username, Password) ->
|
||||||
execute(check, [Username, Password]).
|
execute(check, [Username, Password]).
|
||||||
|
@ -58,15 +64,15 @@ delete(Username) ->
|
||||||
execute(delete, [Username]).
|
execute(delete, [Username]).
|
||||||
|
|
||||||
execute(F, Args) ->
|
execute(F, Args) ->
|
||||||
[{_, M}] = ets:lookup(emqtt_auth, mod),
|
[{_, M}] = ets:lookup(?TAB, mod),
|
||||||
apply(M, F, Args).
|
apply(M, F, Args).
|
||||||
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
{ok, {Name, Opts}} = application:get_env(auth),
|
{ok, {Name, Opts}} = application:get_env(auth),
|
||||||
AuthMod = authmod(Name),
|
AuthMod = authmod(Name),
|
||||||
ok = AuthMod:init(Opts),
|
ok = AuthMod:init(Opts),
|
||||||
ets:new(emqtt_auth, [named_table, protected]),
|
ets:new(?TAB, [named_table, protected]),
|
||||||
ets:insert(emqtt_quth, {mod, AuthMod}),
|
ets:insert(?TAB, {mod, AuthMod}),
|
||||||
?PRINT("emqtt authmod is ~p", [AuthMod]),
|
?PRINT("emqtt authmod is ~p", [AuthMod]),
|
||||||
{ok, undefined}.
|
{ok, undefined}.
|
||||||
|
|
||||||
|
|
|
@ -242,7 +242,7 @@ process_request(?CONNECT,
|
||||||
keep_alive = AlivePeriod,
|
keep_alive = AlivePeriod,
|
||||||
client_id = ClientId } = Var}, #state{socket = Sock} = State) ->
|
client_id = ClientId } = Var}, #state{socket = Sock} = State) ->
|
||||||
{ReturnCode, State1} =
|
{ReturnCode, State1} =
|
||||||
case {ProtoVersion =:= ?MQTT_PROTO_MAJOR,
|
case {lists:member(ProtoVersion, proplists:get_keys(?PROTOCOL_NAMES)),
|
||||||
valid_client_id(ClientId)} of
|
valid_client_id(ClientId)} of
|
||||||
{false, _} ->
|
{false, _} ->
|
||||||
{?CONNACK_PROTO_VER, State};
|
{?CONNACK_PROTO_VER, State};
|
||||||
|
@ -264,7 +264,7 @@ process_request(?CONNECT,
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
?INFO("recv conn...:~p", [ReturnCode]),
|
?INFO("recv conn...:~p", [ReturnCode]),
|
||||||
send_frame(Sock, #mqtt_frame{ fixed = #mqtt_frame_fixed{ type = ?CONNACK},
|
send_frame(Sock, #mqtt_frame{ fixed = #mqtt_frame_fixed{ type = ?CONNACK},
|
||||||
variable = #mqtt_frame_connack{
|
variable = #mqtt_frame_connack{
|
||||||
return_code = ReturnCode }}),
|
return_code = ReturnCode }}),
|
||||||
{ok, State1};
|
{ok, State1};
|
||||||
|
@ -428,7 +428,7 @@ stop(Reason, State ) ->
|
||||||
{stop, Reason, State}.
|
{stop, Reason, State}.
|
||||||
|
|
||||||
valid_client_id(ClientId) ->
|
valid_client_id(ClientId) ->
|
||||||
ClientIdLen = length(ClientId),
|
ClientIdLen = size(ClientId),
|
||||||
1 =< ClientIdLen andalso ClientIdLen =< ?CLIENT_ID_MAXLEN.
|
1 =< ClientIdLen andalso ClientIdLen =< ?CLIENT_ID_MAXLEN.
|
||||||
|
|
||||||
handle_retained(?PUBLISH, #mqtt_frame{fixed = #mqtt_frame_fixed{retain = false}}) ->
|
handle_retained(?PUBLISH, #mqtt_frame{fixed = #mqtt_frame_fixed{retain = false}}) ->
|
||||||
|
|
|
@ -161,7 +161,7 @@ parse_utf(Bin, _) ->
|
||||||
parse_utf(Bin).
|
parse_utf(Bin).
|
||||||
|
|
||||||
parse_utf(<<Len:16/big, Str:Len/binary, Rest/binary>>) ->
|
parse_utf(<<Len:16/big, Str:Len/binary, Rest/binary>>) ->
|
||||||
{binary_to_list(Str), Rest}.
|
{Str, Rest}.
|
||||||
|
|
||||||
parse_msg(Bin, 0) ->
|
parse_msg(Bin, 0) ->
|
||||||
{undefined, Bin};
|
{undefined, Bin};
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
|
|
||||||
-include("emqtt.hrl").
|
-include("emqtt.hrl").
|
||||||
|
|
||||||
|
-include("emqtt_log.hrl").
|
||||||
|
|
||||||
-import(proplists, [get_value/2, get_value/3]).
|
-import(proplists, [get_value/2, get_value/3]).
|
||||||
|
|
||||||
-export([handle/1]).
|
-export([handle/1]).
|
||||||
|
@ -43,8 +45,8 @@ handle(Req) ->
|
||||||
|
|
||||||
handle('POST', "/mqtt/publish", Req) ->
|
handle('POST', "/mqtt/publish", Req) ->
|
||||||
Params = mochiweb_request:parse_post(Req),
|
Params = mochiweb_request:parse_post(Req),
|
||||||
error_logger:info_msg("~p~n", [Params]),
|
?INFO("~p~n", [Params]),
|
||||||
Topic = get_value("topic", Params),
|
Topic = list_to_binary(get_value("topic", Params)),
|
||||||
Message = list_to_binary(get_value("message", Params)),
|
Message = list_to_binary(get_value("message", Params)),
|
||||||
emqtt_pubsub:publish(#mqtt_msg {
|
emqtt_pubsub:publish(#mqtt_msg {
|
||||||
retain = 0,
|
retain = 0,
|
||||||
|
@ -66,12 +68,9 @@ authorized(Req) ->
|
||||||
undefined ->
|
undefined ->
|
||||||
false;
|
false;
|
||||||
"Basic " ++ BasicAuth ->
|
"Basic " ++ BasicAuth ->
|
||||||
{Username, Password} = user_passwd(BasicAuth),
|
emqtt_auth:check(user_passwd(BasicAuth))
|
||||||
emqtt_auth:check(Username, Password)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
user_passwd(BasicAuth) ->
|
user_passwd(BasicAuth) ->
|
||||||
[U, P] = binary:split(base64:decode(BasicAuth), <<":">>),
|
list_to_tuple(binary:split(base64:decode(BasicAuth), <<":">>)).
|
||||||
{binary_to_list(U), binary_to_list(P)}.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
|
|
||||||
-author('feng@slimchat.io').
|
-author('feng@slimchat.io').
|
||||||
|
|
||||||
-export([tcp_name/3, tcp_host/1, getaddr/2, port_to_listeners/1]).
|
-export([tcp_name/3, tcp_host/1, getopts/2, setopts/2, getaddr/2, port_to_listeners/1]).
|
||||||
|
|
||||||
-export([connection_string/2]).
|
-export([connection_string/2]).
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
|
||||||
|
-module(emqtt_protocol).
|
||||||
|
|
||||||
|
-include("emqtt_frame.hrl").
|
||||||
|
|
|
@ -60,12 +60,5 @@ start_child(Mod, Type) when is_atom(Mod) and is_atom(Type) ->
|
||||||
%% ===================================================================
|
%% ===================================================================
|
||||||
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
{ok, { {one_for_all, 5, 10}, [
|
{ok, { {one_for_all, 5, 10}, [] } }.
|
||||||
?CHILD(emqtt_cm, worker),
|
|
||||||
?CHILD(emqtt_monitor, worker),
|
|
||||||
?CHILD(emqtt_auth, worker),
|
|
||||||
?CHILD(emqtt_retained, worker),
|
|
||||||
?CHILD(emqtt_pubsub, worker),
|
|
||||||
?CHILD(emqtt_registry, worker)]}
|
|
||||||
}.
|
|
||||||
|
|
||||||
|
|
|
@ -111,7 +111,7 @@ validate({subscribe, Topic}) when is_binary(Topic) ->
|
||||||
valid(words(Topic));
|
valid(words(Topic));
|
||||||
validate({publish, Topic}) when is_binary(Topic) ->
|
validate({publish, Topic}) when is_binary(Topic) ->
|
||||||
Words = words(Topic),
|
Words = words(Topic),
|
||||||
valid(Words) and (not include_wildcard(Words)).
|
valid(Words) and (not include_wildcard(Topic)).
|
||||||
|
|
||||||
triples(B) when is_binary(B) ->
|
triples(B) when is_binary(B) ->
|
||||||
triples(binary_to_list(B), []).
|
triples(binary_to_list(B), []).
|
||||||
|
@ -152,5 +152,5 @@ include_wildcard(<<$#, _T/binary>>) -> true;
|
||||||
include_wildcard(<<$+, _T/binary>>) -> true;
|
include_wildcard(<<$+, _T/binary>>) -> true;
|
||||||
include_wildcard(<<_H, T/binary>>) -> include_wildcard(T).
|
include_wildcard(<<_H, T/binary>>) -> include_wildcard(T).
|
||||||
|
|
||||||
l2b(L) when is_list(L) -> list_to_binary(L).
|
l2b(L) -> list_to_binary(L).
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue