summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2014-10-25 01:51:46 +0200
committerLinus Nordberg <linus@nordberg.se>2014-10-29 16:21:58 +0100
commitb15f4636337c45b487651e8d442afed0d4141725 (patch)
treee8d79c7d2bff077aad0579681d073f4a412f3eed
parente49af90a5e4470ce3da343708dfbf2e59c745466 (diff)
Move internal HTTP APIs to mochiweb. Stop using jiffy.
Conflicts: src/frontend.erl src/plop.erl src/storage.erl
-rw-r--r--src/frontend.erl141
-rw-r--r--src/plop.erl20
-rw-r--r--src/storage.erl87
3 files changed, 110 insertions, 138 deletions
diff --git a/src/frontend.erl b/src/frontend.erl
index 4043373..a8a8b9e 100644
--- a/src/frontend.erl
+++ b/src/frontend.erl
@@ -5,70 +5,59 @@
-module(frontend).
%% API (URL)
--export([sendlog/3, missingentries/3, sendentry/3, sendsth/3, currentposition/3]).
-
-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)),
-
- ok = db:add(TreeLeafHash, LogEntry),
- binary_to_list(
- jiffy:encode(
- {[{result, <<"ok">>}]}))
- end),
- deliver(SessionID, R).
-
-sendlog(SessionID, _Env, Input) ->
- R = (catch case (catch jiffy:decode(Input)) of
- {error, E} ->
- html("sendentry: bad input:", E);
- {PropList} ->
- Start = proplists:get_value(<<"start">>, PropList),
- Hashes = lists:map(fun (S) -> base64:decode(S) end,
- proplists:get_value(
- <<"hashes">>, PropList)),
-
- Indices = lists:seq(Start, Start + length(Hashes) - 1),
- lists:foreach(fun ({Hash, Index}) ->
- ok = db:add_index(Hash, Index)
- end, lists:zip(Hashes, Indices)),
- binary_to_list(
- jiffy:encode(
- {[{result, <<"ok">>}]}))
- end),
- deliver(SessionID, R).
-
-sendsth(SessionID, _Env, Input) ->
- R = (catch case (catch jiffy:decode(Input)) of
- {error, E} ->
- html("sendentry: bad input:", E);
- {PropList} ->
- Treesize = proplists:get_value(<<"tree_size">>, PropList),
-
- ok = db:set_treesize(Treesize),
-
- ht:reset_tree([db:size() - 1]),
-
- binary_to_list(
- jiffy:encode(
- {[{result, <<"ok">>}]}))
- end),
- deliver(SessionID, R).
-
-currentposition(SessionID, _Env, _Input) ->
+-export([request/3]).
+
+request(post, "ct/frontend/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),
+ success({[{result, <<"ok">>}]})
+ end;
+
+request(post, "ct/frontend/sendlog", Input) ->
+ case (catch mochijson2:decode(Input)) of
+ {error, E} ->
+ html("sendentry: bad input:", E);
+ {struct, PropList} ->
+ Start = proplists:get_value(<<"start">>, PropList),
+ Hashes = lists:map(fun (S) -> base64:decode(S) end, proplists:get_value(<<"hashes">>, PropList)),
+
+ Indices = lists:seq(Start, Start + length(Hashes) - 1),
+ lists:foreach(fun ({Hash, Index}) ->
+ ok = db:add_index(Hash, Index)
+ end, lists:zip(Hashes, Indices)),
+ success({[{result, <<"ok">>}]})
+ end;
+
+request(post, "ct/frontend/sendsth", Input) ->
+ case (catch mochijson2:decode(Input)) of
+ {error, E} ->
+ html("sendentry: bad input:", E);
+ {struct, PropList} ->
+ Treesize = proplists:get_value(<<"tree_size">>, PropList),
+
+ ok = db:set_treesize(Treesize),
+
+ ht:reset_tree([db:size() - 1]),
+
+ success({[{result, <<"ok">>}]})
+ end;
+
+request(get, "ct/frontend/currentposition", _Query) ->
Size = db:size(),
- R = binary_to_list(
- jiffy:encode(
- {[{result, <<"ok">>}, {position, Size}]})),
- deliver(SessionID, R).
+ success({[{result, <<"ok">>},
+ {position, Size}]});
+
+request(get, "ct/frontend/missingentries", _Query) ->
+ Size = db:size(),
+ Missing = fetchmissingentries(Size),
+ success({[{result, <<"ok">>},
+ {entries, Missing}]}).
fetchmissingentries(Index) ->
lists:reverse(fetchmissingentries(Index, [])).
@@ -86,23 +75,15 @@ fetchmissingentries(Index, Acc) ->
end
end.
-missingentries(SessionID, _Env, _Input) ->
- Size = db:size(),
- Missing = fetchmissingentries(Size),
- R = binary_to_list(
- jiffy:encode(
- {[{result, <<"ok">>}, {entries, Missing}]})),
- 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).
+ {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)}.
diff --git a/src/plop.erl b/src/plop.erl
index 4c94f2b..57febf5 100644
--- a/src/plop.erl
+++ b/src/plop.erl
@@ -109,7 +109,8 @@ handle_cast(_Request, State) ->
handle_http_reply(State, {storage_sendentry_http, {OwnRequestId}},
StatusCode, Body) ->
- {PropList} = (catch jiffy:decode(Body)),
+ lager:debug("http_reply: ~p", [Body]),
+ {struct, PropList} = mochijson2:decode(Body),
Result = proplists:get_value(<<"result">>, PropList),
case dict:fetch(OwnRequestId, State#state.own_requests) of
undefined ->
@@ -200,23 +201,24 @@ storage_nodes_quorum() ->
Value.
send_storage_sendentry(URLBase, LogEntry, TreeLeafHash) ->
- Request = jiffy:encode(
+ Request = mochijson2:encode(
{[{plop_version, 1},
{entry, base64:encode(LogEntry)},
{treeleafhash, base64:encode(TreeLeafHash)}
]}),
+ lager:debug("send sendentry to storage node ~p: ~p", [URLBase, Request]),
httpc:request(post, {URLBase ++ "sendentry", [],
- "text/json", Request},
+ "text/json", list_to_binary(Request)},
[], [{sync, false}]).
send_storage_entrycommitted(URLBase, EntryHash, TreeLeafHash) ->
- Request = jiffy:encode(
- {[{plop_version, 1},
- {entryhash, base64:encode(EntryHash)},
- {treeleafhash, base64:encode(TreeLeafHash)}
- ]}),
+ Request = mochijson2:encode(
+ {[{plop_version, 1},
+ {entryhash, base64:encode(EntryHash)},
+ {treeleafhash, base64:encode(TreeLeafHash)}
+ ]}),
httpc:request(post, {URLBase ++ "entrycommitted", [],
- "text/json", Request},
+ "text/json", list_to_binary(Request)},
[], [{sync, false}]).
store_at_all_nodes(Nodes, {LogEntry, TreeLeafHash, EntryHash}, From, State) ->
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" ++
- "<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])}.
--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)}.