From 1ca369b723fa1c601f51cce871e22db1e18b5e8c Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Fri, 13 Nov 2015 15:59:29 +0100 Subject: Parametrise "application part" of URL. Breaking out "ct/v1" to a separate argument to request(). Good for other applications. --- src/catlfish_web.erl | 21 ++++++++++++++++----- src/v1.erl | 22 ++++++++++++---------- 2 files changed, 28 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/catlfish_web.erl b/src/catlfish_web.erl index e541a9b..ea34928 100644 --- a/src/catlfish_web.erl +++ b/src/catlfish_web.erl @@ -17,8 +17,19 @@ add_auth(Path, {Code, Headers, Data}) -> lager:debug("sent auth header: ~p", [AuthHeader]), {Code, [{"X-Catlfish-Auth", AuthHeader} | Headers], Data}. +split_path([]) -> + {[], []}; +split_path([E]) -> + {E, []}; +split_path(Parts) -> + [Fun | AppRev] = lists:reverse(Parts), + App = string:join(lists:reverse(AppRev), "/"), + {Fun, App}. + loop(Req, Module) -> - "/" ++ Path = Req:get(path), + "/" ++ Path = Req:get(path), % FIXME no need to strip "/" + {Fun, App} = split_path(string:tokens(Path, "/")), + lager:debug("Fun=~s; App=~s;", [Fun, App]), try Starttime = os:timestamp(), AuthHeader = Req:get_header_value("X-Catlfish-Auth"), @@ -34,10 +45,10 @@ loop(Req, Module) -> success -> lager:debug("GET ~p ~p", [Path, Query]), add_auth("/" ++ Path, - Module:request(get, Path, Query)); + Module:request(get, App, Fun, Query)); noauth -> lager:debug("GET ~p ~p", [Path, Query]), - Module:request(get, Path, Query) + Module:request(get, App, Fun, Query) end, lager:debug("GET finished: ~p us", [timer:now_diff(os:timestamp(), Starttime)]), @@ -58,10 +69,10 @@ loop(Req, Module) -> success -> lager:debug("POST ~p ~p", [Path, Body]), add_auth("/" ++ Path, - Module:request(post, Path, Body)); + Module:request(post, App, Fun, Body)); noauth -> lager:debug("POST ~p ~p", [Path, Body]), - Module:request(post, Path, Body) + Module:request(post, App, Fun, Body) end, lager:debug("POST finished: ~p us", [timer:now_diff(os:timestamp(), Starttime)]), diff --git a/src/v1.erl b/src/v1.erl index e066cdd..447b36e 100644 --- a/src/v1.erl +++ b/src/v1.erl @@ -5,7 +5,9 @@ -module(v1). %% API (URL) --export([request/3]). +-export([request/4]). + +-define(APPURL_CT_V1, "ct/v1"). check_valid_sth() -> case plop:sth() of @@ -28,15 +30,15 @@ check_valid_sth() -> end. %% Public functions, i.e. part of URL. -request(post, "ct/v1/add-chain", Input) -> +request(post, ?APPURL_CT_V1, "add-chain", Input) -> check_valid_sth(), add_chain(Input, normal); -request(post, "ct/v1/add-pre-chain", Input) -> +request(post, ?APPURL_CT_V1, "add-pre-chain", Input) -> check_valid_sth(), add_chain(Input, precert); -request(get, "ct/v1/get-sth", _Query) -> +request(get, ?APPURL_CT_V1, "get-sth", _Query) -> check_valid_sth(), case plop:sth() of noentry -> @@ -46,7 +48,7 @@ request(get, "ct/v1/get-sth", _Query) -> success(R) end; -request(get, "ct/v1/get-sth-consistency", Query) -> +request(get, ?APPURL_CT_V1, "get-sth-consistency", Query) -> check_valid_sth(), case lists:sort(Query) of [{"first", FirstInput}, {"second", SecondInput}] -> @@ -65,7 +67,7 @@ request(get, "ct/v1/get-sth-consistency", Query) -> _ -> err400("get-sth-consistency: bad input:", Query) end; -request(get, "ct/v1/get-proof-by-hash", Query) -> +request(get, ?APPURL_CT_V1, "get-proof-by-hash", Query) -> check_valid_sth(), case lists:sort(Query) of [{"hash", HashInput}, {"tree_size", TreeSizeInput}] -> @@ -91,7 +93,7 @@ request(get, "ct/v1/get-proof-by-hash", Query) -> _ -> err400("get-proof-by-hash: bad input:", Query) end; -request(get, "ct/v1/get-entries", Query) -> +request(get, ?APPURL_CT_V1, "get-entries", Query) -> check_valid_sth(), case lists:sort(Query) of [{"end", EndInput}, {"start", StartInput}] -> @@ -105,7 +107,7 @@ request(get, "ct/v1/get-entries", Query) -> _ -> err400("get-entries: bad input:", Query) end; -request(get, "ct/v1/get-entry-and-proof", Query) -> +request(get, ?APPURL_CT_V1, "get-entry-and-proof", Query) -> check_valid_sth(), case lists:sort(Query) of [{"leaf_index", IndexInput}, {"tree_size", TreeSizeInput}] -> @@ -120,14 +122,14 @@ request(get, "ct/v1/get-entry-and-proof", Query) -> _ -> err400("get-entry-and-proof: bad input:", Query) end; -request(get, "ct/v1/get-roots", _Query) -> +request(get, ?APPURL_CT_V1, "get-roots", _Query) -> check_valid_sth(), R = [{certificates, [base64:encode(Der) || Der <- catlfish:update_known_roots()]}], success({R}); -request(_Method, _Path, _) -> +request(_Method, _App, _Fun, _) -> none. %% Private functions. -- cgit v1.1