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.
%%
-define(PROTOCOL_NAMES, [{3, "MQIsdp"}, {4, "MQTT"}]).
-define(PROTOCOL_NAMES, [{3, <<"MQIsdp">>}, {4, <<"MQTT">>}]).
-define(MQTT_PROTO_MAJOR, 3).
-define(MQTT_PROTO_MINOR, 1).

View File

@ -30,7 +30,7 @@
-export([start_link/0,
add/2,
check/2,
check/1, check/2,
delete/1]).
-behavior(gen_server).
@ -42,9 +42,15 @@
terminate/2,
code_change/3]).
-define(TAB, ?MODULE).
start_link() ->
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.
check(Username, Password) ->
execute(check, [Username, Password]).
@ -58,15 +64,15 @@ delete(Username) ->
execute(delete, [Username]).
execute(F, Args) ->
[{_, M}] = ets:lookup(emqtt_auth, mod),
[{_, M}] = ets:lookup(?TAB, mod),
apply(M, F, Args).
init([]) ->
{ok, {Name, Opts}} = application:get_env(auth),
AuthMod = authmod(Name),
ok = AuthMod:init(Opts),
ets:new(emqtt_auth, [named_table, protected]),
ets:insert(emqtt_quth, {mod, AuthMod}),
ets:new(?TAB, [named_table, protected]),
ets:insert(?TAB, {mod, AuthMod}),
?PRINT("emqtt authmod is ~p", [AuthMod]),
{ok, undefined}.

View File

@ -242,7 +242,7 @@ process_request(?CONNECT,
keep_alive = AlivePeriod,
client_id = ClientId } = Var}, #state{socket = Sock} = State) ->
{ReturnCode, State1} =
case {ProtoVersion =:= ?MQTT_PROTO_MAJOR,
case {lists:member(ProtoVersion, proplists:get_keys(?PROTOCOL_NAMES)),
valid_client_id(ClientId)} of
{false, _} ->
{?CONNACK_PROTO_VER, State};
@ -264,7 +264,7 @@ process_request(?CONNECT,
end
end,
?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{
return_code = ReturnCode }}),
{ok, State1};
@ -428,7 +428,7 @@ stop(Reason, State ) ->
{stop, Reason, State}.
valid_client_id(ClientId) ->
ClientIdLen = length(ClientId),
ClientIdLen = size(ClientId),
1 =< ClientIdLen andalso ClientIdLen =< ?CLIENT_ID_MAXLEN.
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(<<Len:16/big, Str:Len/binary, Rest/binary>>) ->
{binary_to_list(Str), Rest}.
{Str, Rest}.
parse_msg(Bin, 0) ->
{undefined, Bin};

View File

@ -26,6 +26,8 @@
-include("emqtt.hrl").
-include("emqtt_log.hrl").
-import(proplists, [get_value/2, get_value/3]).
-export([handle/1]).
@ -43,8 +45,8 @@ handle(Req) ->
handle('POST', "/mqtt/publish", Req) ->
Params = mochiweb_request:parse_post(Req),
error_logger:info_msg("~p~n", [Params]),
Topic = get_value("topic", Params),
?INFO("~p~n", [Params]),
Topic = list_to_binary(get_value("topic", Params)),
Message = list_to_binary(get_value("message", Params)),
emqtt_pubsub:publish(#mqtt_msg {
retain = 0,
@ -66,12 +68,9 @@ authorized(Req) ->
undefined ->
false;
"Basic " ++ BasicAuth ->
{Username, Password} = user_passwd(BasicAuth),
emqtt_auth:check(Username, Password)
emqtt_auth:check(user_passwd(BasicAuth))
end.
user_passwd(BasicAuth) ->
[U, P] = binary:split(base64:decode(BasicAuth), <<":">>),
{binary_to_list(U), binary_to_list(P)}.
list_to_tuple(binary:split(base64:decode(BasicAuth), <<":">>)).

View File

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

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([]) ->
{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)]}
}.
{ok, { {one_for_all, 5, 10}, [] } }.

View File

@ -111,7 +111,7 @@ validate({subscribe, Topic}) when is_binary(Topic) ->
valid(words(Topic));
validate({publish, Topic}) when is_binary(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(binary_to_list(B), []).
@ -152,5 +152,5 @@ include_wildcard(<<$#, _T/binary>>) -> true;
include_wildcard(<<$+, _T/binary>>) -> true;
include_wildcard(<<_H, T/binary>>) -> include_wildcard(T).
l2b(L) when is_list(L) -> list_to_binary(L).
l2b(L) -> list_to_binary(L).