From b15f4636337c45b487651e8d442afed0d4141725 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Sat, 25 Oct 2014 01:51:46 +0200 Subject: Move internal HTTP APIs to mochiweb. Stop using jiffy. Conflicts: src/frontend.erl src/plop.erl src/storage.erl --- src/storage.erl | 87 +++++++++++++++++++++++++-------------------------------- 1 file changed, 38 insertions(+), 49 deletions(-) (limited to 'src/storage.erl') diff --git a/src/storage.erl b/src/storage.erl index 243cc6c..e09acdb 100644 --- a/src/storage.erl +++ b/src/storage.erl @@ -5,41 +5,42 @@ -module(storage). %% API (URL) --export([sendentry/3, entrycommitted/3, fetchnewentries/3]). +-export([request/3]). newentries_path() -> {ok, Value} = application:get_env(plop, newentries_path), Value. -sendentry(SessionID, _Env, Input) -> - R = (catch case (catch jiffy:decode(Input)) of - {error, E} -> - html("sendentry: bad input:", E); - {PropList} -> - LogEntry = base64:decode(proplists:get_value(<<"entry">>, PropList)), - TreeLeafHash = base64:decode(proplists:get_value(<<"treeleafhash">>, PropList)), +request(post, "ct/storage/sendentry", Input) -> + case (catch mochijson2:decode(Input)) of + {error, E} -> + html("sendentry: bad input:", E); + {struct, PropList} -> + LogEntry = base64:decode(proplists:get_value(<<"entry">>, PropList)), + TreeLeafHash = base64:decode(proplists:get_value(<<"treeleafhash">>, PropList)), - ok = db:add(TreeLeafHash, LogEntry), - ok = index:addlast(newentries_path(), TreeLeafHash), - binary_to_list( - jiffy:encode( - {[{result, <<"ok">>}]})) - end), - deliver(SessionID, R). - -entrycommitted(SessionID, _Env, Input) -> - R = (catch case (catch jiffy:decode(Input)) of - {error, E} -> - html("entrycommitted: bad input:", E); - {PropList} -> - EntryHash = base64:decode(proplists:get_value(<<"entryhash">>, PropList)), - LeafHash = base64:decode(proplists:get_value(<<"treeleafhash">>, PropList)), - ok = db:add_entryhash(LeafHash, EntryHash), - binary_to_list( - jiffy:encode( - {[{result, <<"ok">>}]})) - end), - deliver(SessionID, R). + ok = db:add(TreeLeafHash, LogEntry), + ok = index:addlast(newentries_path(), TreeLeafHash), + success({[{result, <<"ok">>}]}) + end; +request(post, "ct/storage/entrycommitted", Input) -> + case (catch mochijson2:decode(Input)) of + {error, E} -> + html("entrycommitted: bad input:", E); + {struct, PropList} -> + EntryHash = base64:decode(proplists:get_value(<<"entryhash">>, PropList)), + LeafHash = base64:decode(proplists:get_value(<<"treeleafhash">>, PropList)), + ok = db:add_entryhash(LeafHash, EntryHash), + success({[{result, <<"ok">>}]}) + end; +request(get, "ct/storage/fetchnewentries", _Input) -> + NewHashes = fetchnewhashes(0), + Entries = lists:map(fun(LeafHash) -> + {[{hash, base64:encode(LeafHash)}, + {entry, base64:encode(db:entry_for_leafhash(LeafHash))}]} + end, NewHashes), + success({[{result, <<"ok">>}, + {entries, Entries}]}). fetchnewhashes(Index) -> lists:reverse(fetchnewhashes(Index, [])). @@ -52,26 +53,14 @@ fetchnewhashes(Index, Acc) -> fetchnewhashes(Index + 1, [Entry | Acc]) end. -fetchnewentries(SessionID, _Env, _Input) -> - NewHashes = fetchnewhashes(0), - Entries = lists:map(fun(LeafHash) -> - {[{hash, base64:encode(LeafHash)}, - {entry, base64:encode(db:entry_for_leafhash(LeafHash))}]} - end, NewHashes), - R = (catch binary_to_list( - jiffy:encode( - {[{result, <<"ok">>}, {entries, Entries}]}))), - deliver(SessionID, R). - %% Private functions. html(Text, Input) -> - io_lib:format( - "Content-Type: text/html\r\n\r\n" ++ - "

~n" ++ - "~s~n" ++ - "~p~n" ++ - "~n", [Text, Input]). + {400, [{"Content-Type", "text/html"}], + io_lib:format( + "

~n" ++ + "~s~n" ++ + "~p~n" ++ + "~n", [Text, Input])}. --spec deliver(any(), string()) -> ok | {error, _Reason}. -deliver(Session, Data) -> - mod_esi:deliver(Session, Data). +success(Data) -> + {200, [{"Content-Type", "text/json"}], mochijson2:encode(Data)}. -- cgit v1.1