From c74bacfee30f114f20de391e252df6c40127d8f3 Mon Sep 17 00:00:00 2001 From: Maciej Szlosarczyk Date: Tue, 28 May 2019 11:05:51 +0300 Subject: [PATCH] Move epp_record into a header file --- apps/epp_proxy/include/epp_proxy.hrl | 8 ++++++++ apps/epp_proxy/src/epp_tcp_worker.erl | 19 ++++++++++--------- apps/epp_proxy/src/epp_tls_worker.erl | 20 +++++++++++--------- 3 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 apps/epp_proxy/include/epp_proxy.hrl diff --git a/apps/epp_proxy/include/epp_proxy.hrl b/apps/epp_proxy/include/epp_proxy.hrl new file mode 100644 index 0000000..d887f21 --- /dev/null +++ b/apps/epp_proxy/include/epp_proxy.hrl @@ -0,0 +1,8 @@ +%% This record is used by both epp_tcp_worker and epp_tls_worker. +-record(epp_request, + {method, % get | post (atom) + url, % "https://example.com/some-url" + body, % "" | {multipart [{{<<"raw_frame">>, "Some body"}}]} + cookies, % "" | {multipart [{{<<"raw_frame">>, "Some body"}}]} + headers % [{"User-Agent", <<"EPP proxy">>}, {"Other", <<"Header">>}] + }). diff --git a/apps/epp_proxy/src/epp_tcp_worker.erl b/apps/epp_proxy/src/epp_tcp_worker.erl index 7de16ec..61d7b56 100644 --- a/apps/epp_proxy/src/epp_tcp_worker.erl +++ b/apps/epp_proxy/src/epp_tcp_worker.erl @@ -3,12 +3,13 @@ -behaviour(gen_server). -define(SERVER, ?MODULE). +-include("epp_proxy.hrl"). + %% gen_server callbacks -export([init/1, handle_cast/2, handle_call/3, start_link/1]). -export([code_change/3]). -record(state,{socket, length, session_id}). --record(request,{method, url, body, cookies, headers}). init(Socket) -> logger:info("Created a worker process"), @@ -25,9 +26,9 @@ handle_cast(greeting, State = #state{socket=Socket, session_id=SessionId}) -> logger:info("Request: ~p~n", [Request]), {_Status, _StatusCode, _Headers, ClientRef} = - hackney:request(Request#request.method, Request#request.url, - Request#request.headers, Request#request.body, - [{cookie, Request#request.cookies}, insecure]), + hackney:request(Request#epp_request.method, Request#epp_request.url, + Request#epp_request.headers, Request#epp_request.body, + [{cookie, Request#epp_request.cookies}, insecure]), {ok, Body} = hackney:body(ClientRef), @@ -58,9 +59,9 @@ handle_cast(process_command, State = #state{socket=Socket, session_id=SessionId} logger:info("Request: ~p~n", [Request]), {_Status, _StatusCode, _Headers, ClientRef} = - hackney:request(Request#request.method, Request#request.url, - Request#request.headers, Request#request.body, - [{cookie, Request#request.cookies}, insecure]), + hackney:request(Request#epp_request.method, Request#epp_request.url, + Request#epp_request.headers, Request#epp_request.body, + [{cookie, Request#epp_request.cookies}, insecure]), {ok, Body} = hackney:body(ClientRef), @@ -115,8 +116,8 @@ request(Command, SessionId, RawFrame) -> _ -> Body = {multipart, [{<<"raw_frame">>, RawFrame}]} end, - Headers = [], - #request{url=URL, method=RequestMethod, body=Body, cookies=[Cookie], + Headers = [{"User-Agent", <<"EPP proxy">>}], + #epp_request{url=URL, method=RequestMethod, body=Body, cookies=[Cookie], headers=Headers}. %% Wrap a message in EPP frame, and then send it to socket. diff --git a/apps/epp_proxy/src/epp_tls_worker.erl b/apps/epp_proxy/src/epp_tls_worker.erl index 76dfe5b..ada1409 100644 --- a/apps/epp_proxy/src/epp_tls_worker.erl +++ b/apps/epp_proxy/src/epp_tls_worker.erl @@ -3,6 +3,8 @@ -behaviour(gen_server). -define(SERVER, ?MODULE). +-include("epp_proxy.hrl"). + %% gen_server callbacks -export([init/1, handle_cast/2, handle_call/3, start_link/1]). -export([code_change/3]). @@ -10,7 +12,6 @@ -export([request/5]). -record(state,{socket, length, session_id, common_name, client_cert}). --record(request,{method, url, body, cookies, headers}). init(Socket) -> logger:info("Created a worker process"), @@ -36,9 +37,9 @@ handle_cast(greeting, State = #state{socket=Socket, common_name=SSL_CLIENT_S_DN_ logger:info("Request: ~p~n", [Request]), {_Status, _StatusCode, _Headers, ClientRef} = - hackney:request(Request#request.method, Request#request.url, - Request#request.headers, Request#request.body, - [{cookie, Request#request.cookies}, insecure]), + hackney:request(Request#epp_request.method, Request#epp_request.url, + Request#epp_request.headers, Request#epp_request.body, + [{cookie, Request#epp_request.cookies}, insecure]), {ok, Body} = hackney:body(ClientRef), @@ -72,9 +73,9 @@ handle_cast(process_command, State = #state{socket=Socket, logger:info("Request: ~p~n", [Request]), {_Status, _StatusCode, _Headers, ClientRef} = - hackney:request(Request#request.method, Request#request.url, - Request#request.headers, Request#request.body, - [{cookie, Request#request.cookies}, insecure]), + hackney:request(Request#epp_request.method, Request#epp_request.url, + Request#epp_request.headers, Request#epp_request.body, + [{cookie, Request#epp_request.cookies}, insecure]), {ok, Body} = hackney:body(ClientRef), @@ -129,8 +130,9 @@ request(Command, SessionId, RawFrame, CommonName, ClientCert) -> Body = {multipart, [{<<"raw_frame">>, RawFrame}]} end, Headers = [{"SSL_CLIENT_CERT", ClientCert}, - {"SSL_CLIENT_S_DN_CN", CommonName}], - #request{url=URL, method=RequestMethod, body=Body, cookies=[Cookie], + {"SSL_CLIENT_S_DN_CN", CommonName}, + {"User-Agent", <<"EPP proxy">>}], + #epp_request{url=URL, method=RequestMethod, body=Body, cookies=[Cookie], headers=Headers}. %% Wrap a message in EPP frame, and then send it to socket.