summaryrefslogtreecommitdiff
path: root/src/storage.erl
blob: 5774f2a5044ff8ef80fd11433a899519a93f8bb5 (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
%%% Copyright (c) 2014-2015, NORDUnet A/S.
%%% See LICENSE for licensing information.

%%% @doc Storage node API

-module(storage).
%% API (URL)
-export([request/3]).

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 = storagedb:add(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)),
            db:add_entryhash(LeafHash, EntryHash),
            success({[{result, <<"ok">>}]})
    end;
request(get, "ct/storage/fetchnewentries", _Input) ->
    {LastIndexOrZero, LastHash} = storagedb:lastverifiednewentry(),
    LastVerifiedAndNewHashes = storagedb:fetchnewhashes(LastIndexOrZero),
    NewHashes = case {LastHash, LastVerifiedAndNewHashes} of
                    {none, LastVerifiedAndNewHashes} ->
                        LastVerifiedAndNewHashes;
                    {Hash1, [Hash2|Rest]} when Hash1 == Hash2 ->
                        Rest;
                    _ ->
                        exit({internalerror, "Incorrect lastverifiedentry"})
                end,
    Entries = lists:map(fun(LeafHash) ->
                                base64:encode(LeafHash)
                        end, NewHashes),
    success({[{result, <<"ok">>},
              {entries, Entries}]});
request(get, "ct/storage/getentry", Query) ->
    Hashes = [base64:decode(Value) || {Key, Value} <- Query, Key == "hash"],
    Entries = lists:map(fun(LeafHash) ->
                                {[{hash, base64:encode(LeafHash)},
                                  {entry, base64:encode(case db:entry_for_leafhash(LeafHash) of
                                                            noentry ->
                                                                <<>>;
                                                            Entry ->
                                                                Entry
                                                        end)}]}
                        end, Hashes),
    success({[{result, <<"ok">>},
              {entries, Entries}]}).

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

success(Data) ->
    {200, [{"Content-Type", "text/json"}], mochijson2:encode(Data)}.