summaryrefslogtreecommitdiff
path: root/src/v1.erl
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2014-10-24 04:37:18 +0200
committerMagnus Ahltorp <map@kth.se>2014-10-24 04:37:18 +0200
commit314370d6e5e6326033b648ba621803b75337935f (patch)
tree355221c58ced5429db7e226107394e8794e0b706 /src/v1.erl
parent3e43d0c819e95a75b850c298cfe9904ed180ac16 (diff)
Use mochiweb for v1 APIexternal-merge
Diffstat (limited to 'src/v1.erl')
-rw-r--r--src/v1.erl84
1 files changed, 42 insertions, 42 deletions
diff --git a/src/v1.erl b/src/v1.erl
index 535fc8f..71210a0 100644
--- a/src/v1.erl
+++ b/src/v1.erl
@@ -5,12 +5,10 @@
-module(v1).
%% API (URL)
--export(['add-chain'/3, 'add-pre-chain'/3, 'get-sth'/3,
- 'get-sth-consistency'/3, 'get-proof-by-hash'/3, 'get-entries'/3,
- 'get-roots'/3, 'get-entry-and-proof'/3]).
+-export([request/3]).
%% Public functions, i.e. part of URL.
-'add-chain'(SessionID, _Env, Input) ->
+request(post, "ct/v1/add-chain", Input) ->
R = case (catch jiffy:decode(Input)) of
{error, E} ->
html("add-chain: bad input:", E);
@@ -26,12 +24,12 @@
end;
_ -> html("add-chain: missing input: chain", Input)
end,
- deliver(SessionID, R).
+ success(R);
-'add-pre-chain'(SessionID, _Env, _Input) ->
- niy(SessionID).
+request(post, "ct/v1/add-pre-chain", _Input) ->
+ niy();
-'get-sth'(SessionID, _Env, _Input) ->
+request(get, "ct/v1/get-sth", _Query) ->
{ Treesize,
Timestamp,
Roothash,
@@ -41,10 +39,10 @@
{sha256_root_hash, base64:encode(Roothash)},
{tree_head_signature, base64:encode(
plop:serialise(Signature))}],
- deliver(SessionID, binary_to_list(jiffy:encode({R}))).
+ success(jiffy:encode({R}));
-'get-sth-consistency'(SessionID, _Env, Input) ->
- R = case lists:sort(httpd:parse_query(Input)) of
+request(get, "ct/v1/get-sth-consistency", Query) ->
+ R = case lists:sort(Query) of
[{"first", FirstInput}, {"second", SecondInput}] ->
{First, _} = string:to_integer(FirstInput),
{Second, _} = string:to_integer(SecondInput),
@@ -53,18 +51,18 @@
html("get-sth-consistency: bad input:",
[FirstInput, SecondInput]);
false ->
- binary_to_list(
+ success(
jiffy:encode(
{[{consistency,
[base64:encode(X) ||
X <- plop:consistency(First, Second)]}]}))
end;
- _ -> html("get-sth-consistency: bad input:", Input)
+ _ -> html("get-sth-consistency: bad input:", Query)
end,
- deliver(SessionID, R).
+ R;
-'get-proof-by-hash'(SessionID, _Env, Input) ->
- R = case lists:sort(httpd:parse_query(Input)) of
+request(get, "ct/v1/get-proof-by-hash", Query) ->
+ R = case lists:sort(Query) of
[{"hash", HashInput}, {"tree_size", TreeSizeInput}] ->
Hash = case (catch base64:decode(HashInput)) of
{'EXIT', _} -> error;
@@ -76,7 +74,7 @@
html("get-proof-by-hash: bad input:",
[HashInput, TreeSizeInput]);
false ->
- binary_to_list(
+ success(
jiffy:encode(
case plop:inclusion(Hash, TreeSize) of
{ok, Index, Path} ->
@@ -89,25 +87,25 @@
{error_message, list_to_binary(Msg)}]}
end))
end;
- _ -> html("get-proof-by-hash: bad input:", Input)
+ _ -> html("get-proof-by-hash: bad input:", Query)
end,
- deliver(SessionID, R).
+ R;
-'get-entries'(SessionID, _Env, Input) ->
- R = case lists:sort(httpd:parse_query(Input)) of
+request(get, "ct/v1/get-entries", Query) ->
+ R = case lists:sort(Query) of
[{"end", EndInput}, {"start", StartInput}] ->
{Start, _} = string:to_integer(StartInput),
{End, _} = string:to_integer(EndInput),
case lists:member(error, [Start, End]) of
true -> html("get-entries: bad input:", [Start, End]);
- false -> catlfish:entries(Start, min(End, Start + 999))
+ false -> success(catlfish:entries(Start, min(End, Start + 999)))
end;
- _ -> html("get-entries: bad input:", Input)
+ _ -> html("get-entries: bad input:", Query)
end,
- deliver(SessionID, R).
+ R;
-'get-entry-and-proof'(SessionID, _Env, Input) ->
- R = case lists:sort(httpd:parse_query(Input)) of
+request(get, "ct/v1/get-entry-and-proof", Query) ->
+ R = case lists:sort(Query) of
[{"leaf_index", IndexInput}, {"tree_size", TreeSizeInput}] ->
{Index, _} = string:to_integer(IndexInput),
{TreeSize, _} = string:to_integer(TreeSizeInput),
@@ -115,28 +113,30 @@
true ->
html("get-entry-and-proof: not integers: ",
[IndexInput, TreeSizeInput]);
- false -> catlfish:entry_and_proof(Index, TreeSize)
+ false -> success(catlfish:entry_and_proof(Index, TreeSize))
end;
- _ -> html("get-entry-and-proof: bad input:", Input)
+ _ -> html("get-entry-and-proof: bad input:", Query)
end,
- deliver(SessionID, R).
+ R;
-'get-roots'(SessionID, _Env, _Input) ->
+request(get, "ct/v1/get-roots", _Query) ->
R = [{certificates, []}], % NIY.
- deliver(SessionID, binary_to_list(jiffy:encode({R}))).
+ success(jiffy:encode({R}));
+
+request(_Method, _Path, _) ->
+ none.
%% Private functions.
html(Text, Input) ->
- io_lib:format(
- "Content-Type: text/html\r\n\r\n" ++
- "<html><body><p>~n" ++
- "~s~n" ++
- "~p~n" ++
- "</body></html>~n", [Text, Input]).
+ {400, [{"Content-Type", "text/html"}],
+ io_lib:format(
+ "<html><body><p>~n" ++
+ "~s~n" ++
+ "~p~n" ++
+ "</body></html>~n", [Text, Input])}.
-niy(S) ->
- mod_esi:deliver(S, html("NIY - Not Implemented Yet|", [])).
+niy() ->
+ html("NIY - Not Implemented Yet|", []).
--spec deliver(any(), string()) -> ok | {error, _Reason}.
-deliver(Session, Data) ->
- mod_esi:deliver(Session, Data).
+success(Data) ->
+ {200, [{"Content-Type", "text/json"}], Data}.