]> granicus.if.org Git - ejabberd/commitdiff
Use httpc directly instead of using p1_http wrapper
authorPaweł Chmielowski <pchmielowski@process-one.net>
Mon, 23 Apr 2018 15:40:44 +0000 (17:40 +0200)
committerPaweł Chmielowski <pchmielowski@process-one.net>
Mon, 23 Apr 2018 15:40:44 +0000 (17:40 +0200)
src/ext_mod.erl
src/rest.erl

index f042997abce89a824c31cd33f5c11dda4eb0f015..275a80e4a47e1698d39b734dea453d8bf8f01f31 100644 (file)
@@ -56,7 +56,8 @@ init([]) ->
     process_flag(trap_exit, true),
     [code:add_patha(module_ebin_dir(Module))
      || {Module, _} <- installed()],
-    p1_http:start(),
+    application:start(inets),
+    inets:start(httpc, [{profile, ext_mod}]),
     ejabberd_commands:register_commands(get_commands_spec()),
     {ok, #state{}}.
 
@@ -313,23 +314,22 @@ check(Package) when is_binary(Package) ->
 %% -- archives and variables functions
 
 geturl(Url) ->
-    geturl(Url, []).
-geturl(Url, UsrOpts) ->
-    geturl(Url, [], UsrOpts).
-geturl(Url, Hdrs, UsrOpts) ->
-    Host = case getenv("PROXY_SERVER", "", ":") of
-        [H, Port] -> [{proxy_host, H}, {proxy_port, list_to_integer(Port)}];
-        [H] -> [{proxy_host, H}, {proxy_port, 8080}];
-        _ -> []
+    case getenv("PROXY_SERVER", "", ":") of
+        [H, Port] ->
+            httpc:set_options([{proxy, {{H, list_to_integer(Port)}, []}}], ext_mod);
+        [H] ->
+            httpc:set_options([{proxy, {{H, 8080}, []}}], ext_mod);
+        _ ->
+            ok
     end,
     User = case getenv("PROXY_USER", "", [4]) of
-        [U, Pass] -> [{proxy_user, U}, {proxy_password, Pass}];
+        [U, Pass] -> [{proxy_auth, {U, Pass}}];
         _ -> []
     end,
-    case p1_http:request(get, Url, Hdrs, [], Host++User++UsrOpts++[{version, "HTTP/1.0"}]) of
-        {ok, 200, Headers, Response} ->
+    case httpc:request(get, {Url, []}, User, [{body_format, binary}], ext_mod) of
+        {ok, {{_, 200, _}, Headers, Response}} ->
             {ok, Headers, Response};
-        {ok, Code, _Headers, Response} ->
+        {ok, {{_, Code, _}, _Headers, Response}} ->
             {error, {Code, Response}};
         {error, Reason} ->
             {error, Reason}
index 99501ae566f19e4ff393660d5a7080388f03da87..0165d9fa7acec7c02ff92832da3bf209669002b2 100644 (file)
@@ -36,9 +36,9 @@
 -define(CONNECT_TIMEOUT, 8000).
 
 start(Host) ->
-    p1_http:start(),
-    Pool_size = ejabberd_config:get_option({ext_api_http_pool_size, Host}, 100),
-    p1_http:set_pool_size(Pool_size).
+    application:start(inets),
+    Size = ejabberd_config:get_option({ext_api_http_pool_size, Host}, 100),
+    httpc:set_options([{max_sessions, Size}]).
 
 stop(_Host) ->
     ok.
@@ -58,41 +58,46 @@ with_retry(Method, Args, Retries, MaxRetries, Backoff) ->
     end.
 
 get(Server, Path) ->
-    request(Server, get, Path, [], <<"application/json">>, <<>>).
+    request(Server, get, Path, [], "application/json", <<>>).
 get(Server, Path, Params) ->
-    request(Server, get, Path, Params, <<"application/json">>, <<>>).
+    request(Server, get, Path, Params, "application/json", <<>>).
 
 delete(Server, Path) ->
-    request(Server, delete, Path, [], <<"application/json">>, <<>>).
+    request(Server, delete, Path, [], "application/json", <<>>).
 
 post(Server, Path, Params, Content) ->
     Data = encode_json(Content),
-    request(Server, post, Path, Params, <<"application/json">>, Data).
+    request(Server, post, Path, Params, "application/json", Data).
 
 put(Server, Path, Params, Content) ->
     Data = encode_json(Content),
-    request(Server, put, Path, Params, <<"application/json">>, Data).
+    request(Server, put, Path, Params, "application/json", Data).
 
 patch(Server, Path, Params, Content) ->
     Data = encode_json(Content),
-    request(Server, patch, Path, Params, <<"application/json">>, Data).
+    request(Server, patch, Path, Params, "application/json", Data).
 
 request(Server, Method, Path, Params, Mime, Data) ->
-    URI = url(Server, Path, Params),
+    URI = to_list(url(Server, Path, Params)),
     Opts = [{connect_timeout, ?CONNECT_TIMEOUT},
             {timeout, ?HTTP_TIMEOUT}],
-    Hdrs = [{<<"connection">>, <<"keep-alive">>},
-           {<<"content-type">>, Mime},
-           {<<"User-Agent">>, <<"ejabberd">>}],
+    Hdrs = [{"connection", "keep-alive"},
+           {"User-Agent", "ejabberd"}],
+    Req = if
+              (Method =:= post) orelse (Method =:= patch) orelse (Method =:= put) orelse (Method =:= delete) ->
+                  {URI, Hdrs, to_list(Mime), Data};
+              true ->
+                  {URI, Hdrs}
+          end,
     Begin = os:timestamp(),
-    Result = case catch p1_http:request(Method, URI, Hdrs, Data, Opts) of
-        {ok, Code, _, <<>>} ->
+    Result = try httpc:request(Method, Req, Opts, [{body_format, binary}]) of
+        {ok, {{_, Code, _}, _, <<>>}} ->
             {ok, Code, []};
-        {ok, Code, _, <<" ">>} ->
+        {ok, {{_, Code, _}, _, <<" ">>}} ->
             {ok, Code, []};
-        {ok, Code, _, <<"\r\n">>} ->
+        {ok, {{_, Code, _}, _, <<"\r\n">>}} ->
             {ok, Code, []};
-        {ok, Code, _, Body} ->
+        {ok, {{_, Code, _}, _, Body}} ->
             try jiffy:decode(Body) of
                 JSon ->
                     {ok, Code, JSon}
@@ -110,8 +115,9 @@ request(Server, Method, Path, Params, Mime, Data) ->
                        "** URI = ~s~n"
                        "** Err = ~p",
                        [URI, Reason]),
-            {error, {http_error, {error, Reason}}};
-        {'EXIT', Reason} ->
+            {error, {http_error, {error, Reason}}}
+        catch
+        exit:Reason ->
             ?ERROR_MSG("HTTP request failed:~n"
                        "** URI = ~s~n"
                        "** Err = ~p",
@@ -141,6 +147,11 @@ request(Server, Method, Path, Params, Mime, Data) ->
 %%% HTTP helpers
 %%%----------------------------------------------------------------------
 
+to_list(V) when is_binary(V) ->
+    binary_to_list(V);
+to_list(V) ->
+    V.
+
 encode_json(Content) ->
     case catch jiffy:encode(Content) of
         {'EXIT', Reason} ->
@@ -164,7 +175,7 @@ base_url(Server, Path) ->
             Base = ejabberd_config:get_option({ext_api_url, Server},
                                               <<"http://localhost/api">>),
             case binary:last(Base) of
-                47 -> <<Base/binary, BPath/binary>>;
+                $/ -> <<Base/binary, BPath/binary>>;
                 _ -> <<Base/binary, "/", BPath/binary>>
             end
     end,