summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2015-03-02 11:40:17 +0100
committerMagnus Ahltorp <map@kth.se>2015-03-02 11:40:17 +0100
commitefdfd2413bab91fffe2c52154c832f77198093a3 (patch)
treebbb80e58ce6b28aa290a4b36cc81fc5408c0a804 /src
parent25fbc04ed4909e90e7318be4ab5f1ca19b68ac39 (diff)
Add http request function to http_util
Diffstat (limited to 'src')
-rw-r--r--src/http_util.erl50
-rw-r--r--src/plop.erl48
2 files changed, 54 insertions, 44 deletions
diff --git a/src/http_util.erl b/src/http_util.erl
new file mode 100644
index 0000000..53dd2bb
--- /dev/null
+++ b/src/http_util.erl
@@ -0,0 +1,50 @@
+%%% 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 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}}}