commit
1de176a887
|
@ -88,13 +88,13 @@ handle_request(Method, Path, Req) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
http_publish(Req) ->
|
http_publish(Req) ->
|
||||||
Params = mochiweb_request:parse_post(Req),
|
Params = [{iolist_to_binary(Key), Val} || {Key, Val} <- mochiweb_request:parse_post(Req)],
|
||||||
lager:info("HTTP Publish: ~p", [Params]),
|
lager:info("HTTP Publish: ~p", [Params]),
|
||||||
Topics = topics(Params),
|
Topics = topics(Params),
|
||||||
ClientId = get_value("client", Params, http),
|
ClientId = get_value(<<"client">>, Params, http),
|
||||||
Qos = int(get_value("qos", Params, "0")),
|
Qos = int(get_value(<<"qos">>, Params, "0")),
|
||||||
Retain = bool(get_value("retain", Params, "0")),
|
Retain = bool(get_value(<<"retain">>, Params, "0")),
|
||||||
Payload = list_to_binary(get_value("message", Params)),
|
Payload = iolist_to_binary(get_value(<<"message">>, Params)),
|
||||||
case {validate(qos, Qos), validate(topics, Topics)} of
|
case {validate(qos, Qos), validate(topics, Topics)} of
|
||||||
{true, true} ->
|
{true, true} ->
|
||||||
lists:foreach(fun(Topic) ->
|
lists:foreach(fun(Topic) ->
|
||||||
|
@ -109,8 +109,8 @@ http_publish(Req) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
topics(Params) ->
|
topics(Params) ->
|
||||||
Tokens = [get_value("topic", Params) | string:tokens(get_value("topics", Params, ""), ",")],
|
Tokens = [get_value(<<"topic">>, Params) | string:tokens(get_value(<<"topics">>, Params, ""), ",")],
|
||||||
[list_to_binary(Token) || Token <- Tokens, Token =/= undefined].
|
[iolist_to_binary(Token) || Token <- Tokens, Token =/= undefined].
|
||||||
|
|
||||||
validate(qos, Qos) ->
|
validate(qos, Qos) ->
|
||||||
(Qos >= ?QOS_0) and (Qos =< ?QOS_2);
|
(Qos >= ?QOS_0) and (Qos =< ?QOS_2);
|
||||||
|
@ -151,10 +151,16 @@ authorized(Req) ->
|
||||||
user_passwd(BasicAuth) ->
|
user_passwd(BasicAuth) ->
|
||||||
list_to_tuple(binary:split(base64:decode(BasicAuth), <<":">>)).
|
list_to_tuple(binary:split(base64:decode(BasicAuth), <<":">>)).
|
||||||
|
|
||||||
|
int(I) when is_integer(I)-> I;
|
||||||
|
int(B) when is_binary(B)-> binary_to_integer(B);
|
||||||
int(S) -> list_to_integer(S).
|
int(S) -> list_to_integer(S).
|
||||||
|
|
||||||
|
bool(0) -> false;
|
||||||
|
bool(1) -> true;
|
||||||
bool("0") -> false;
|
bool("0") -> false;
|
||||||
bool("1") -> true.
|
bool("1") -> true;
|
||||||
|
bool(<<"0">>) -> false;
|
||||||
|
bool(<<"1">>) -> true.
|
||||||
|
|
||||||
is_websocket(Upgrade) ->
|
is_websocket(Upgrade) ->
|
||||||
Upgrade =/= undefined andalso string:to_lower(Upgrade) =:= "websocket".
|
Upgrade =/= undefined andalso string:to_lower(Upgrade) =:= "websocket".
|
||||||
|
|
Loading…
Reference in New Issue