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