diff --git a/src/hijack.erl b/src/hijack.erl index 3e47cbf64c1cea9074bdea4d4d260ed2ca0d6d70..f05b578525793ff4520272c4d8c6405aa785794e 100644 --- a/src/hijack.erl +++ b/src/hijack.erl @@ -59,14 +59,23 @@ loop(State = #state{consul_socket = Socket}) -> {error, _Reason} -> gen_tcp:close(Socket) end; - {response, From, Id, Status, Headers, Body} -> + {response, From, Status, Headers, Body} -> loop(send_to_consul(State, #{ <<"from">> => From, - <<"id">> => Id, <<"status">> => Status, <<"headers">> => maps:from_list(Headers), <<"body">> => Body })); + {gun_timeout, _Pid, From} -> + loop(send_to_consul(State, #{ + <<"from">> => From, + <<"error">> => <<"timeout">> + })); + {gun_error, _Pid, From, Reason} -> + loop(send_to_consul(State, #{ + <<"from">> => From, + <<"error">> => list_to_binary(io_lib:format("~p", [Reason])) + })); Msg -> io:format("loop recv ~p~n", [Msg]), loop(State) @@ -91,7 +100,8 @@ handle(State = #state{opts = Opts}, #{<<"action">> := <<"authorized">>, <<"usern send_to_consul(State, #{ <<"action">> => <<"bind">>, <<"host">> => list_to_binary(proplists:get_value(host, Opts, "")), - <<"path">> => list_to_binary(proplists:get_value(path, Opts, "")) + <<"path">> => list_to_binary(proplists:get_value(path, Opts, "")), + <<"mode">> => <<"hijack">> }); handle(State, #{<<"action">> := <<"bind">>, <<"host">> := Host, <<"path">> := Path}) -> @@ -103,7 +113,7 @@ handle(State = #state{consul_socket = Socket}, #{<<"action">> := <<"error">>, << io:format("~s~n", [Message]), State#state{consul_socket = undefined}; -handle(State = #state{target_host = Host, target_port = Port}, Request = #{<<"id">> := _Id}) -> +handle(State = #state{target_host = Host, target_port = Port}, Request = #{<<"from">> := _From}) -> Parent = self(), spawn( fun() -> @@ -116,38 +126,39 @@ handle(State = #state{target_host = Host, target_port = Port}, Request = #{<<"id handle(State, #{<<"action">> := <<"pong">>}) -> State; -handle(State, _Terms) -> +handle(State, Terms) -> + io:format("~p~n", [Terms]), State. -gun_loop(Parent, Request = #{<<"id">> := Id, <<"from">> := From}, Pid, StreamRef) -> +gun_loop(Parent, Request = #{<<"from">> := From}, Pid, StreamRef) -> receive {gun_up, Pid, _Protocol} -> - Parent ! {gun_up, Id}, + Parent ! {gun_up, Pid}, gun_loop(Parent, Request, Pid, request(Pid, Request)); {gun_down, Pid, _Protocol, _Reason, _, _} -> - Parent ! {gun_down, Id}; + Parent ! {gun_down, Pid}; {gun_error, Pid, StreamRef, Reason} -> - Parent ! {gun_error, Id, Reason}; + Parent ! {gun_error, Pid, From, Reason}; {gun_response, Pid, StreamRef, fin, Status, Headers} -> - Parent ! {response, From, Id, Status, Headers, <<>>}; + Parent ! {response, From, Status, Headers, <<>>}; {gun_response, Pid, StreamRef, nofin, Status, Headers} -> gun_loop_data(Parent, Request, Pid, StreamRef, Status, Headers, <<>>) after 1000 -> - Parent ! {gun_timeout, Id} + Parent ! {gun_timeout, Pid, From} end. -gun_loop_data(Parent, Request = #{<<"id">> := Id, <<"from">> := From}, Pid, StreamRef, Status, Headers, Buffer) -> +gun_loop_data(Parent, Request = #{<<"from">> := From}, Pid, StreamRef, Status, Headers, Buffer) -> receive {gun_down, Pid, _Protocol, _Reason, _, _} -> - Parent ! {gun_down, Id}; + Parent ! {gun_down, Pid}; {gun_error, Pid, StreamRef, Reason} -> - Parent ! {gun_error, Id, Reason}; + Parent ! {gun_error, Pid, From, Reason}; {gun_data, Pid, StreamRef, nofin, Data} -> gun_loop_data(Parent, Request, Pid, StreamRef, Status, Headers, <<Buffer/binary, Data/binary>>); {gun_data, Pid, StreamRef, fin, Data} -> - Parent ! {response, From, Id, Status, Headers, <<Buffer/binary, Data/binary>>} + Parent ! {response, From, Status, Headers, <<Buffer/binary, Data/binary>>} after 3000 -> - Parent ! {gun, Pid, timeout} + Parent ! {gun_timeout, Pid, From} end. request(Pid, Request = #{<<"method">> := <<"GET">>}) ->