summaryrefslogtreecommitdiff
path: root/src/storage.erl
blob: b966a12571bb2e7006b2d0e699e795d85fe1933e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
%%% Copyright (c) 2014, NORDUnet A/S.
%%% See LICENSE for licensing information.

%%% @doc Storage node API

-module(storage).
%% API (URL)
-export([sendentry/3, entrycommitted/3, fetchnewentries/3]).

newentries_path() ->
    {ok, Value} = application:get_env(plop, newentries_path),
    Value.

sendentry(SessionID, _Env, Input) ->
    lager:debug("~p", [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)),

                       ok = db:add(TreeLeafHash, LogEntry),
                       ok = index:addlast(newentries_path(), TreeLeafHash),
                       binary_to_list(
                         jiffy:encode(
                           {[{result, <<"ok">>}]}))
               end),
    lager:debug("result ~p", [R]),
    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).

fetchnewhashes(Index) ->
    lists:reverse(fetchnewhashes(Index, [])).

fetchnewhashes(Index, Acc) ->
    case index:get(newentries_path(), Index) of
        noentry ->
            Acc;
        Entry ->
            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" ++
          "<html><body><p>~n" ++
          "~s~n" ++
          "~p~n" ++
          "</body></html>~n", [Text, Input]).

-spec deliver(any(), string()) -> ok | {error, _Reason}.
deliver(Session, Data) ->
    mod_esi:deliver(Session, Data).