Skip to content

Commit

Permalink
Merge pull request #284 from emqx/fix/qs
Browse files Browse the repository at this point in the history
fix(qs): support query string parameters in path for webhook
  • Loading branch information
tigercl committed Jun 28, 2021
2 parents 28436cc + 27cb608 commit 7cc1977
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
10 changes: 4 additions & 6 deletions src/emqx_web_hook.appup.src
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

{VSN,
[
{<<"4.2.[0-5]">>, [
{<<".*">>, [
{restart_application, emqx_web_hook}
]},
{<<".*">>, []}
]}
],
[
{<<"4.2.[0-5]">>, [
{<<".*">>, [
{restart_application, emqx_web_hook}
]},
{<<".*">>, []}
]}
]
}.
14 changes: 11 additions & 3 deletions src/emqx_web_hook_actions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ parse_action_params(Params = #{<<"url">> := URL}) ->
try
#{path := CommonPath} = uri_string:parse(add_default_scheme(URL)),
#{method => method(maps:get(<<"method">>, Params, <<"POST">>)),
path => path(filename:join(CommonPath, maps:get(<<"path">>, Params, <<>>))),
path => merge_path(CommonPath, maps:get(<<"path">>, Params, <<>>)),
headers => headers(maps:get(<<"headers">>, Params, undefined)),
content_type => maps:get(<<"content_type">>, Params, <<"application/json">>),
payload_tmpl => maps:get(<<"payload_tmpl">>, Params, <<>>),
Expand All @@ -270,8 +270,16 @@ parse_action_params(Params = #{<<"url">> := URL}) ->
throw({invalid_params, Params})
end.

path(<<>>) -> <<"/">>;
path(Path) -> Path.
merge_path(CommonPath, <<>>) ->
CommonPath;
merge_path(CommonPath, Path0) ->
case uri_string:parse(Path0) of
#{path := Path1, 'query' := Query} ->
Path2 = filename:join(CommonPath, Path1),
<<Path2/binary, "?", Query/binary>>;
#{path := Path1} ->
filename:join(CommonPath, Path1)
end.

method(GET) when GET == <<"GET">>; GET == <<"get">> -> get;
method(POST) when POST == <<"POST">>; POST == <<"post">> -> post;
Expand Down
11 changes: 7 additions & 4 deletions src/emqx_web_hook_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ add_default_scheme(URL) ->
translate_env() ->
{ok, URL} = application:get_env(?APP, url),
#{host := Host0,
path := Path0,
scheme := Scheme} = URIMap = uri_string:parse(add_default_scheme(URL)),
Port = maps:get(port, URIMap, case Scheme of
"https" -> 443;
_ -> 80
end),
Path = path(Path0),
Path = path(URIMap),
PoolSize = application:get_env(?APP, pool_size, 32),
{Inet, Host} = parse_host(Host0),
MoreOpts = case Scheme of
Expand Down Expand Up @@ -96,9 +95,13 @@ translate_env() ->
NHeaders = set_content_type(Headers),
application:set_env(?APP, headers, NHeaders).

path("") ->
path(#{path := "", 'query' := Query}) ->
"?" ++ Query;
path(#{path := Path, 'query' := Query}) ->
Path ++ "?" ++ Query;
path(#{path := ""}) ->
"/";
path(Path) ->
path(#{path := Path}) ->
Path.

set_content_type(Headers) ->
Expand Down

0 comments on commit 7cc1977

Please sign in to comment.