From 35a43b7b1951fdb45e2367122a3928ddba49c894 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Mon, 2 Mar 2015 11:44:50 +0100 Subject: Rename http_util plop_httputil --- src/http_util.erl | 50 -------------------------------------------------- src/plop.erl | 2 +- src/plop_httputil.erl | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 51 deletions(-) delete mode 100644 src/http_util.erl create mode 100644 src/plop_httputil.erl (limited to 'src') diff --git a/src/http_util.erl b/src/http_util.erl deleted file mode 100644 index 53dd2bb..0000000 --- a/src/http_util.erl +++ /dev/null @@ -1,50 +0,0 @@ -%%% Copyright (c) 2014, NORDUnet A/S. -%%% See LICENSE for licensing information. -%%% - --module(http_util). --export([request/4]). --include_lib("hackney/include/hackney_lib.hrl"). - -get_auth_header(Headers) -> - case hackney_headers:get_value("X-Catlfish-Auth", Headers) of - undefined -> - undefined; - Result when is_binary(Result) -> - lager:debug("received auth header: ~p", [Result]), - binary_to_list(Result) - end. - -add_auth(Method, Path, Headers, Data) -> - AuthHeader = http_auth:create_auth(Method, Path, Data), - lager:debug("sent auth header: ~p", [AuthHeader]), - [{"X-Catlfish-Auth", AuthHeader} | Headers]. - -request(DebugTag, URL, Headers, RequestBody) -> - Starttime = os:timestamp(), - ParsedURL = hackney_url:parse_url(URL), - CACertFile = application:get_env(catlfish, https_cacertfile, none), - #hackney_url{path = Path} = ParsedURL, - lager:debug("~s: sending http request to ~p", - [DebugTag, URL]), - {ok, ConnRef} = hackney:connect(ParsedURL, [{ssl_options, [{cacertfile, CACertFile}]}]), - lager:debug("~s: connected to ~p", - [DebugTag, URL]), - {ok, StatusCode, RespHeaders, ClientRef} = - hackney:send_request(ConnRef, - {post, Path, - add_auth("POST", Path, Headers, - RequestBody), - RequestBody}), - lager:debug("~s: received headers for ~p: ~p", - [DebugTag, URL, RespHeaders]), - {ok, Body} = hackney:body(ClientRef), - Stoptime = os:timestamp(), - hackney:close(ClientRef), - lager:debug("~s: received body for ~p: time ~p", - [DebugTag, URL, timer:now_diff(Stoptime, Starttime)]), - StatusLine = {none, StatusCode, none}, - AuthHeader = get_auth_header(hackney_headers:new(RespHeaders)), - {http_auth:verify_auth(AuthHeader, "REPLY", - binary_to_list(Path), Body), - StatusLine, RespHeaders, Body}. diff --git a/src/plop.erl b/src/plop.erl index 0b95faa..63579d1 100644 --- a/src/plop.erl +++ b/src/plop.erl @@ -197,7 +197,7 @@ send_http_request(TreeLeafHash, URL, Headers, RequestBody) -> ParentPid = self(), RequestId = make_ref(), spawn(fun () -> - case http_util:request("leafhash " ++ mochihex:to_hex(TreeLeafHash), URL, Headers, RequestBody) of + case plop_httputil:request("leafhash " ++ mochihex:to_hex(TreeLeafHash), URL, Headers, RequestBody) of {failure, StatusLine, RespHeaders, Body} -> lager:debug("auth check failed"), drop; diff --git a/src/plop_httputil.erl b/src/plop_httputil.erl new file mode 100644 index 0000000..a7be8b5 --- /dev/null +++ b/src/plop_httputil.erl @@ -0,0 +1,50 @@ +%%% Copyright (c) 2014, NORDUnet A/S. +%%% See LICENSE for licensing information. +%%% + +-module(plop_httputil). +-export([request/4]). +-include_lib("hackney/include/hackney_lib.hrl"). + +get_auth_header(Headers) -> + case hackney_headers:get_value("X-Catlfish-Auth", Headers) of + undefined -> + undefined; + Result when is_binary(Result) -> + lager:debug("received auth header: ~p", [Result]), + binary_to_list(Result) + end. + +add_auth(Method, Path, Headers, Data) -> + AuthHeader = http_auth:create_auth(Method, Path, Data), + lager:debug("sent auth header: ~p", [AuthHeader]), + [{"X-Catlfish-Auth", AuthHeader} | Headers]. + +request(DebugTag, URL, Headers, RequestBody) -> + Starttime = os:timestamp(), + ParsedURL = hackney_url:parse_url(URL), + CACertFile = application:get_env(catlfish, https_cacertfile, none), + #hackney_url{path = Path} = ParsedURL, + lager:debug("~s: sending http request to ~p", + [DebugTag, URL]), + {ok, ConnRef} = hackney:connect(ParsedURL, [{ssl_options, [{cacertfile, CACertFile}]}]), + lager:debug("~s: connected to ~p", + [DebugTag, URL]), + {ok, StatusCode, RespHeaders, ClientRef} = + hackney:send_request(ConnRef, + {post, Path, + add_auth("POST", Path, Headers, + RequestBody), + RequestBody}), + lager:debug("~s: received headers for ~p: ~p", + [DebugTag, URL, RespHeaders]), + {ok, Body} = hackney:body(ClientRef), + Stoptime = os:timestamp(), + hackney:close(ClientRef), + lager:debug("~s: received body for ~p: time ~p", + [DebugTag, URL, timer:now_diff(Stoptime, Starttime)]), + StatusLine = {none, StatusCode, none}, + AuthHeader = get_auth_header(hackney_headers:new(RespHeaders)), + {http_auth:verify_auth(AuthHeader, "REPLY", + binary_to_list(Path), Body), + StatusLine, RespHeaders, Body}. -- cgit v1.1