fix httpc
This commit is contained in:
parent
8b9774132b
commit
b30a8a91df
@ -82,23 +82,28 @@ handle_call(_Request, _From, State = #state{}) ->
|
|||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
handle_cast({download, ReceiverPid, Ref, Url, TargetDir}, State = #state{}) ->
|
handle_cast({download, ReceiverPid, Ref, Url, TargetDir}, State = #state{}) ->
|
||||||
SSLOpts = {ssl_options, [
|
SslOpts = [
|
||||||
% 完全禁用证书验证
|
{ssl, [
|
||||||
{verify, verify_none}
|
% 完全禁用证书验证
|
||||||
]},
|
{verify, verify_none}
|
||||||
|
]}
|
||||||
|
],
|
||||||
|
|
||||||
TargetFile = get_filename_from_url(Url),
|
TargetFile = get_filename_from_url(Url),
|
||||||
|
FullFilename = TargetDir ++ TargetFile,
|
||||||
|
|
||||||
StartTs = os:timestamp(),
|
StartTs = os:timestamp(),
|
||||||
case hackney:request(get, Url, [], <<>>, [async, {stream_to, self()}, {pool, false}, SSLOpts]) of
|
case httpc:request(get, {Url, []}, SslOpts, [{sync, false}, {stream, self}]) of
|
||||||
{ok, ClientRef} ->
|
{ok, RequestId} ->
|
||||||
case receive_data(ClientRef, TargetDir, TargetFile) of
|
case receive_data(RequestId, FullFilename) of
|
||||||
ok ->
|
ok ->
|
||||||
EndTs = os:timestamp(),
|
EndTs = os:timestamp(),
|
||||||
%% 计算操作的时间,单位为毫秒
|
%% 计算操作的时间,单位为毫秒
|
||||||
CostMs = timer:now_diff(EndTs, StartTs) div 1000,
|
CostMs = timer:now_diff(EndTs, StartTs) div 1000,
|
||||||
efka_logger:debug("download cost: ~p", [CostMs]),
|
|
||||||
ReceiverPid ! {download_response, Ref, {ok, CostMs}};
|
ReceiverPid ! {download_response, Ref, {ok, CostMs}};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
|
%% 出错需要删除掉文件
|
||||||
|
file:delete(FullFilename),
|
||||||
ReceiverPid ! {download_response, Ref, {error, Reason}}
|
ReceiverPid ! {download_response, Ref, {error, Reason}}
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
@ -137,37 +142,25 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
|
|||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
||||||
%% 读取请求 ResponseLine
|
|
||||||
receive_data(ClientRef, TargetDir, DefaultFile) when is_list(TargetDir), is_list(DefaultFile) ->
|
|
||||||
receive
|
|
||||||
{hackney_response, ClientRef, {status, 200, _Reason}} ->
|
|
||||||
receive_data0(ClientRef, TargetDir, DefaultFile);
|
|
||||||
{hackney_response, ClientRef, {status, StatusCode, _Reason}} ->
|
|
||||||
{error, {http_status, StatusCode}}
|
|
||||||
end.
|
|
||||||
%% 处理头部信息, 解析可能的文件名
|
%% 处理头部信息, 解析可能的文件名
|
||||||
receive_data0(ClientRef, TargetDir, DefaultFile) ->
|
receive_data(RequestId, FullFilename) ->
|
||||||
receive
|
receive
|
||||||
{hackney_response, ClientRef, {headers, Headers}} ->
|
{http, {RequestId, stream_start, _Headers}} ->
|
||||||
TargetFilename = extra_filename(Headers, DefaultFile),
|
|
||||||
FullFilename = TargetDir ++ TargetFilename,
|
|
||||||
efka_logger:debug("full name: ~p", [FullFilename]),
|
|
||||||
{ok, File} = file:open(FullFilename, [write, binary]),
|
{ok, File} = file:open(FullFilename, [write, binary]),
|
||||||
receive_data1(ClientRef, File)
|
receive_data1(RequestId, File)
|
||||||
end.
|
end.
|
||||||
%% 接受文件数据
|
%% 接受文件数据
|
||||||
receive_data1(ClientRef, File) ->
|
receive_data1(RequestId, File) ->
|
||||||
receive
|
receive
|
||||||
{hackney_response, ClientRef, {error, Reason}} ->
|
{http, {RequestId, {error, Reason}}} ->
|
||||||
ok = file:close(File),
|
ok = file:close(File),
|
||||||
{error, Reason};
|
{error, Reason};
|
||||||
{hackney_response, ClientRef, done} ->
|
{http, {RequestId, stream_end, _Headers}} ->
|
||||||
ok = file:close(File),
|
ok = file:close(File),
|
||||||
hackney:close(ClientRef),
|
|
||||||
ok;
|
ok;
|
||||||
{hackney_response, ClientRef, Data} ->
|
{http, {RequestId, stream, Data}} ->
|
||||||
file:write(File, Data),
|
file:write(File, Data),
|
||||||
receive_data1(ClientRef, File)
|
receive_data1(RequestId, File)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec extra_filename(Headers :: list(), Default :: string()) -> Filename :: string().
|
-spec extra_filename(Headers :: list(), Default :: string()) -> Filename :: string().
|
||||||
@ -192,4 +185,3 @@ get_filename_from_url(Url) when is_list(Url) ->
|
|||||||
URIMap = uri_string:parse(Url),
|
URIMap = uri_string:parse(Url),
|
||||||
Path = maps:get(path, URIMap),
|
Path = maps:get(path, URIMap),
|
||||||
filename:basename(Path).
|
filename:basename(Path).
|
||||||
|
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
-export([debug/2, notice/2]).
|
-export([debug/2, notice/2]).
|
||||||
|
|
||||||
debug(Format, Args) ->
|
debug(Format, Args) ->
|
||||||
io:format(Format, Args).
|
io:format(Format ++ "~n", Args).
|
||||||
|
|
||||||
notice(Format, Args) ->
|
notice(Format, Args) ->
|
||||||
io:format(Format, Args).
|
io:format(Format ++ "~n", Args).
|
||||||
|
|||||||
@ -28,14 +28,14 @@ start_link() ->
|
|||||||
init([]) ->
|
init([]) ->
|
||||||
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
|
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
|
||||||
ChildSpecs = [
|
ChildSpecs = [
|
||||||
#{
|
%#{
|
||||||
id => 'efka_agent',
|
% id => 'efka_agent',
|
||||||
start => {'efka_agent', start_link, []},
|
% start => {'efka_agent', start_link, []},
|
||||||
restart => permanent,
|
% restart => permanent,
|
||||||
shutdown => 2000,
|
% shutdown => 2000,
|
||||||
type => worker,
|
% type => worker,
|
||||||
modules => ['efka_agent']
|
% modules => ['efka_agent']
|
||||||
},
|
%},
|
||||||
|
|
||||||
#{
|
#{
|
||||||
id => 'efka_server_sup',
|
id => 'efka_server_sup',
|
||||||
|
|||||||
@ -35,8 +35,6 @@
|
|||||||
]
|
]
|
||||||
}]}]}.
|
}]}]}.
|
||||||
|
|
||||||
{erl_opts, [{parse_transform,lager_transform}]}.
|
|
||||||
|
|
||||||
{project_plugins, [
|
{project_plugins, [
|
||||||
%% 或从 Git 仓库拉取最新版本
|
%% 或从 Git 仓库拉取最新版本
|
||||||
{pc, {git, "https://github.com/blt/port_compiler.git", {tag, "v1.15.0"}}}
|
{pc, {git, "https://github.com/blt/port_compiler.git", {tag, "v1.15.0"}}}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user