summaryrefslogtreecommitdiff
path: root/src/plop_httputil.erl
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2015-03-02 11:44:50 +0100
committerMagnus Ahltorp <map@kth.se>2015-03-02 15:35:24 +0100
commit35a43b7b1951fdb45e2367122a3928ddba49c894 (patch)
tree8e57ca1909e442b87965c147319a1c215abb8311 /src/plop_httputil.erl
parentefdfd2413bab91fffe2c52154c832f77198093a3 (diff)
Rename http_util plop_httputil
Diffstat (limited to 'src/plop_httputil.erl')
-rw-r--r--src/plop_httputil.erl50
1 files changed, 50 insertions, 0 deletions
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}.