mirror of
https://github.com/internetee/epp_proxy.git
synced 2025-08-26 08:53:28 +02:00
Move http request into its own module
This commit is contained in:
parent
54a6fdb729
commit
0ccb48ce91
4 changed files with 100 additions and 20 deletions
|
@ -12,7 +12,17 @@
|
||||||
url, % "https://example.com/some-url"
|
url, % "https://example.com/some-url"
|
||||||
query_params, % {[{<<"msg">>, <<"Some">>}, {<<"code">>, <<"2001">>}]}
|
query_params, % {[{<<"msg">>, <<"Some">>}, {<<"code">>, <<"2001">>}]}
|
||||||
cookies, % [<<"session=SomeSession; Version=1">>]
|
cookies, % [<<"session=SomeSession; Version=1">>]
|
||||||
headers % [{"User-Agent", <<"EPP proxy">>}, {"Other", <<"Header">>}]
|
headers % [{"User-Agent", <<"EPP proxy">>}, {"Other", <<"Header">>}]
|
||||||
|
}).
|
||||||
|
|
||||||
|
%% Unified version of the two records above. Depending on the method,
|
||||||
|
%% it either encodes parameters into multipart form or query parameters and url.
|
||||||
|
-record(epp_unified_request,
|
||||||
|
{method, % get
|
||||||
|
url, % "https://example.com/some-url"
|
||||||
|
params, % {[{<<"msg">>, <<"Some">>}, {<<"code">>, <<"2001">>}]}
|
||||||
|
cookies, % [<<"session=SomeSession; Version=1">>]
|
||||||
|
headers % [{"User-Agent", <<"EPP proxy">>}, {"Other", <<"Header">>}]
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-type epp_request() :: #epp_request{}.
|
-type epp_request() :: #epp_request{}.
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
-behaviour(epp_http_client_behaviour).
|
-behaviour(epp_http_client_behaviour).
|
||||||
|
|
||||||
-export([request/1, error_request/1]).
|
-export([request/1, error_request/1, request_builder/1]).
|
||||||
|
|
||||||
%% Callback API
|
%% Callback API
|
||||||
request(#epp_request{} = Request) ->
|
request(#epp_request{} = Request) ->
|
||||||
|
@ -21,6 +21,9 @@ error_request(#epp_error_request{} = Request) ->
|
||||||
{ok, Body} = hackney:body(ClientRef),
|
{ok, Body} = hackney:body(ClientRef),
|
||||||
{Status, Body}.
|
{Status, Body}.
|
||||||
|
|
||||||
|
request_builder(Map) ->
|
||||||
|
request_from_map(Map).
|
||||||
|
|
||||||
%% Private API
|
%% Private API
|
||||||
-spec handle_args(epp_request()) -> list().
|
-spec handle_args(epp_request()) -> list().
|
||||||
handle_args(#epp_request{method=get, url=URL, headers=Headers, body="",
|
handle_args(#epp_request{method=get, url=URL, headers=Headers, body="",
|
||||||
|
@ -36,3 +39,66 @@ handle_error_args(#epp_error_request{method=get, url=URL, headers=Headers,
|
||||||
QueryString = hackney_url:qs(Params),
|
QueryString = hackney_url:qs(Params),
|
||||||
CompleteURL = [URL, <<"?">>, QueryString],
|
CompleteURL = [URL, <<"?">>, QueryString],
|
||||||
[get, CompleteURL, Headers, "", [{cookie, Cookies}, insecure]].
|
[get, CompleteURL, Headers, "", [{cookie, Cookies}, insecure]].
|
||||||
|
|
||||||
|
|
||||||
|
%% Map request and return values.
|
||||||
|
request_from_map(#{command := "error", session_id := SessionId,
|
||||||
|
code := Code, message := Message, headers:=Headers,
|
||||||
|
cl_trid := ClTRID}) ->
|
||||||
|
URL = epp_router:route_request("error"),
|
||||||
|
RequestMethod = epp_router:request_method("error"),
|
||||||
|
Cookie = hackney_cookie:setcookie("session", SessionId, []),
|
||||||
|
QueryParams = query_params(Code, Message, ClTRID),
|
||||||
|
Headers=Headers,
|
||||||
|
Request = #epp_error_request{url=URL,
|
||||||
|
method=RequestMethod,
|
||||||
|
query_params=QueryParams,
|
||||||
|
cookies=[Cookie],
|
||||||
|
headers=Headers},
|
||||||
|
lager:info("Error Request from map: [~p]~n", [Request]),
|
||||||
|
Request;
|
||||||
|
request_from_map(#{command := Command, session_id := SessionId,
|
||||||
|
raw_frame := RawFrame, headers:=Headers, cl_trid := ClTRID}) ->
|
||||||
|
URL = epp_router:route_request(Command),
|
||||||
|
RequestMethod = epp_router:request_method(Command),
|
||||||
|
Cookie = hackney_cookie:setcookie("session", SessionId, []),
|
||||||
|
Body = request_body(Command, RawFrame, ClTRID),
|
||||||
|
Headers=Headers,
|
||||||
|
Request = #epp_request{url=URL,
|
||||||
|
method=RequestMethod,
|
||||||
|
body=Body,
|
||||||
|
cookies=[Cookie],
|
||||||
|
headers=Headers},
|
||||||
|
lager:info("Request from map: [~p]~n", [Request]),
|
||||||
|
Request;
|
||||||
|
request_from_map(#{command := Command, session_id := SessionId,
|
||||||
|
raw_frame := RawFrame, common_name := CommonName,
|
||||||
|
client_cert := ClientCert, peer_ip := PeerIp, cl_trid := ClTRID}) ->
|
||||||
|
URL = epp_router:route_request(Command),
|
||||||
|
RequestMethod = epp_router:request_method(Command),
|
||||||
|
Cookie = hackney_cookie:setcookie("session", SessionId, []),
|
||||||
|
Body = request_body(Command, RawFrame, ClTRID),
|
||||||
|
Headers = [{"SSL_CLIENT_CERT", ClientCert},
|
||||||
|
{"SSL_CLIENT_S_DN_CN", CommonName},
|
||||||
|
{"User-Agent", <<"EPP proxy">>},
|
||||||
|
{"X-Forwarded-for", epp_util:readable_ip(PeerIp)}],
|
||||||
|
Request = #epp_request{url=URL,
|
||||||
|
method=RequestMethod,
|
||||||
|
body=Body,
|
||||||
|
cookies=[Cookie],
|
||||||
|
headers=Headers},
|
||||||
|
lager:info("Request from map: [~p]~n", [Request]),
|
||||||
|
Request.
|
||||||
|
|
||||||
|
%% Return form data or an empty list.
|
||||||
|
request_body("hello", _, _) ->
|
||||||
|
"";
|
||||||
|
request_body(_Command, RawFrame, nomatch) ->
|
||||||
|
{multipart, [{<<"raw_frame">>, RawFrame}]};
|
||||||
|
request_body(_Command, RawFrame, ClTRID) ->
|
||||||
|
{multipart, [{<<"raw_frame">>, RawFrame}, {<<"clTRID">>, ClTRID}]}.
|
||||||
|
|
||||||
|
query_params(Code, Message, nomatch) ->
|
||||||
|
[{<<"code">>, Code}, {<<"msg">>, Message}];
|
||||||
|
query_params(Code, Message, ClTRID) ->
|
||||||
|
[{<<"code">>, Code}, {<<"msg">>, Message}, {<<"clTRID">>, ClTRID}].
|
||||||
|
|
|
@ -7,3 +7,5 @@
|
||||||
%% Abstract module for http client behaviour. It should call EPP HTTP server
|
%% Abstract module for http client behaviour. It should call EPP HTTP server
|
||||||
%% and return a response back to the caller.
|
%% and return a response back to the caller.
|
||||||
-callback request(epp_request()) -> http_response().
|
-callback request(epp_request()) -> http_response().
|
||||||
|
-callback error_request(epp_error_request()) -> http_response().
|
||||||
|
-callback request_builder(map()) -> epp_request() | epp_error_request().
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
headers }).
|
headers }).
|
||||||
|
|
||||||
init(Socket) ->
|
init(Socket) ->
|
||||||
lager:info("Created a worker process"),
|
lager:info("Created a worker process: [~p]", [self()]),
|
||||||
SessionId = epp_util:session_id(self()),
|
SessionId = epp_util:session_id(self()),
|
||||||
{ok, #state{socket=Socket, session_id=SessionId}}.
|
{ok, #state{socket=Socket, session_id=SessionId}}.
|
||||||
|
|
||||||
|
@ -40,17 +40,20 @@ handle_cast(greeting, State = #state{socket=Socket,
|
||||||
session_id=SessionId,
|
session_id=SessionId,
|
||||||
headers=Headers}) ->
|
headers=Headers}) ->
|
||||||
|
|
||||||
Request = request_from_map(#{command => "hello",
|
Request = epp_http_client:request_builder(#{command => "hello",
|
||||||
session_id => SessionId,
|
session_id => SessionId,
|
||||||
raw_frame => "",
|
raw_frame => "",
|
||||||
headers => Headers,
|
headers => Headers,
|
||||||
cl_trid => nomatch}),
|
cl_trid => nomatch}),
|
||||||
|
|
||||||
{_Status, Body} = epp_http_client:request(Request),
|
{_Status, Body} = epp_http_client:request(Request),
|
||||||
|
|
||||||
frame_to_socket(Body, Socket),
|
frame_to_socket(Body, Socket),
|
||||||
gen_server:cast(self(), process_command),
|
gen_server:cast(self(), process_command),
|
||||||
{noreply, State#state{socket=Socket, session_id=SessionId}};
|
{noreply, State#state{socket=Socket, session_id=SessionId}};
|
||||||
|
|
||||||
|
%% Main loop of processing commands. Ends the connection when command is logout.
|
||||||
|
%%
|
||||||
handle_cast(process_command,
|
handle_cast(process_command,
|
||||||
State = #state{socket=Socket,session_id=SessionId,
|
State = #state{socket=Socket,session_id=SessionId,
|
||||||
headers=Headers}) ->
|
headers=Headers}) ->
|
||||||
|
@ -58,21 +61,21 @@ handle_cast(process_command,
|
||||||
|
|
||||||
case parse_frame(RawFrame) of
|
case parse_frame(RawFrame) of
|
||||||
#valid_frame{command=Command, cl_trid=ClTRID} ->
|
#valid_frame{command=Command, cl_trid=ClTRID} ->
|
||||||
Request = request_from_map(#{command => Command,
|
Request = epp_http_client:request_builder(#{command => Command,
|
||||||
session_id => SessionId,
|
session_id => SessionId,
|
||||||
raw_frame => RawFrame,
|
raw_frame => RawFrame,
|
||||||
headers => Headers,
|
headers => Headers,
|
||||||
cl_trid => ClTRID}),
|
cl_trid => ClTRID}),
|
||||||
|
|
||||||
{_Status, Body} = epp_http_client:request(Request);
|
{_Status, Body} = epp_http_client:request(Request);
|
||||||
#invalid_frame{message=Message, code=Code, cl_trid=ClTRID} ->
|
#invalid_frame{message=Message, code=Code, cl_trid=ClTRID} ->
|
||||||
Command = "error",
|
Command = "error",
|
||||||
Request = request_from_map(#{command => Command,
|
Request = epp_http_client:request_builder(#{command => Command,
|
||||||
session_id => SessionId,
|
session_id => SessionId,
|
||||||
headers => Headers,
|
headers => Headers,
|
||||||
code => Code,
|
code => Code,
|
||||||
message => Message,
|
message => Message,
|
||||||
cl_trid => ClTRID}),
|
cl_trid => ClTRID}),
|
||||||
{_Status, Body} = epp_http_client:error_request(Request)
|
{_Status, Body} = epp_http_client:error_request(Request)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
@ -197,7 +200,6 @@ frame_from_socket(Socket, State) ->
|
||||||
|
|
||||||
Frame = case read_frame(Socket, Length) of
|
Frame = case read_frame(Socket, Length) of
|
||||||
{ok, FrameData} ->
|
{ok, FrameData} ->
|
||||||
io:format("~p~n", [FrameData]),
|
|
||||||
FrameData;
|
FrameData;
|
||||||
{error, _FrameDetails} ->
|
{error, _FrameDetails} ->
|
||||||
{stop, normal, State}
|
{stop, normal, State}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue