From efdfd2413bab91fffe2c52154c832f77198093a3 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Mon, 2 Mar 2015 11:40:17 +0100 Subject: Add http request function to http_util --- src/plop.erl | 48 ++++-------------------------------------------- 1 file changed, 4 insertions(+), 44 deletions(-) (limited to 'src/plop.erl') diff --git a/src/plop.erl b/src/plop.erl index 6ff65f8..0b95faa 100644 --- a/src/plop.erl +++ b/src/plop.erl @@ -39,7 +39,6 @@ %%-include("db.hrl"). -include_lib("public_key/include/public_key.hrl"). -include_lib("eunit/include/eunit.hrl"). --include_lib("hackney/include/hackney_lib.hrl"). %%%%% moved from plop.hrl, maybe remove -define(PLOPVERSION, 0). @@ -194,58 +193,19 @@ storage_nodes_quorum() -> {ok, Value} = application:get_env(plop, storage_nodes_quorum), Value. -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]. - -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. - send_http_request(TreeLeafHash, URL, Headers, RequestBody) -> ParentPid = self(), RequestId = make_ref(), - CACertFile = application:get_env(catlfish, https_cacertfile, none), spawn(fun () -> - Starttime = os:timestamp(), - ParsedURL = hackney_url:parse_url(URL), - #hackney_url{path = Path} = ParsedURL, - lager:debug("leafhash ~s: sending http request to ~p", - [mochihex:to_hex(TreeLeafHash), URL]), - {ok, ConnRef} = hackney:connect(ParsedURL, [{ssl_options, [{cacertfile, CACertFile}]}]), - lager:debug("leafhash ~s: connected to ~p", - [mochihex:to_hex(TreeLeafHash), URL]), - {ok, StatusCode, RespHeaders, ClientRef} = - hackney:send_request(ConnRef, - {post, Path, - add_auth("POST", Path, Headers, - RequestBody), - RequestBody}), - lager:debug("leafhash ~s: received headers for ~p: ~p", - [mochihex:to_hex(TreeLeafHash), URL, RespHeaders]), - {ok, Body} = hackney:body(ClientRef), - Stoptime = os:timestamp(), - hackney:close(ClientRef), - lager:debug("leafhash ~s: received body for ~p: time ~p", - [mochihex:to_hex(TreeLeafHash), URL, timer:now_diff(Stoptime, Starttime)]), - StatusLine = {none, StatusCode, none}, - AuthHeader = get_auth_header(hackney_headers:new(RespHeaders)), - case http_auth:verify_auth(AuthHeader, "REPLY", - binary_to_list(Path), Body) of - failure -> + case http_util:request("leafhash " ++ mochihex:to_hex(TreeLeafHash), URL, Headers, RequestBody) of + {failure, StatusLine, RespHeaders, Body} -> lager:debug("auth check failed"), drop; - success -> + {success, StatusLine, RespHeaders, Body} -> lager:debug("auth check succeeded"), ParentPid ! {http, {RequestId, {StatusLine, RespHeaders, Body}}}; - noauth -> + {noauth, StatusLine, RespHeaders, Body} -> lager:debug("no auth"), ParentPid ! {http, {RequestId, {StatusLine, RespHeaders, Body}}} -- cgit v1.1