list -> binary

This commit is contained in:
Feng Lee 2014-12-10 19:20:09 +08:00
parent 5fc497ec1a
commit 20fb39cf3b
9 changed files with 30 additions and 27 deletions

View File

@ -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).

View File

@ -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}.

View File

@ -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};
@ -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}}) ->

View File

@ -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};

View File

@ -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)}.

View File

@ -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]).

View File

@ -0,0 +1,5 @@
-module(emqtt_protocol).
-include("emqtt_frame.hrl").

View File

@ -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)]}
}.

View File

@ -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).