summaryrefslogtreecommitdiff
path: root/src/v1.erl
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2014-10-24 04:37:18 +0200
committerLinus Nordberg <linus@nordberg.se>2014-10-24 11:10:05 +0200
commit00156d1b7740c4177fc29bd10d75ea4e0e59cc6e (patch)
tree8dc3ac870fead70b59b40240d7b12734e7d0d67b /src/v1.erl
parent5e0ad5a06c608250ca1130e378932c0275c99e63 (diff)
Use mochiweb for v1 API
Conflicts: catlfish.config src/v1.erl
Diffstat (limited to 'src/v1.erl')
-rw-r--r--src/v1.erl86
1 files changed, 43 insertions, 43 deletions
diff --git a/src/v1.erl b/src/v1.erl
index d5e65ea..0c13cbc 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);
@@ -25,7 +23,7 @@
{ok, [Leaf | Chain]} ->
io:format("[info] adding ~p~n",
[x509:cert_string(LeafCert)]),
- catlfish:add_chain(Leaf, Chain);
+ success(catlfish:add_chain(Leaf, Chain));
{Err, Msg} ->
io:format("[info] rejecting ~p: ~p~n",
[x509:cert_string(LeafCert), Err]),
@@ -36,12 +34,12 @@
end;
_ -> html("add-chain: missing input: chain", Input)
end,
- deliver(SessionID, R).
+ 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,
@@ -51,10 +49,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),
@@ -63,18 +61,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;
@@ -86,7 +84,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} ->
@@ -99,25 +97,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),
@@ -125,30 +123,32 @@
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,
[base64:encode(Der) ||
Der <- catlfish:update_known_roots()]}],
- 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}.