%%% 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}.