Permalink
Browse files

Removed state passing via init... it was not needed.

  • Loading branch information...
1 parent 12089b0 commit 15b7c5aa3ab864581dc3bfedce58ddfd0cc2f96c @mmzeeman mmzeeman committed with Nov 17, 2013
Showing with 13 additions and 47 deletions.
  1. +0 −18 src/elli_example_callback_handover.erl
  2. +12 −14 src/elli_http.erl
  3. +1 −1 src/elli_middleware.erl
  4. +0 −14 test/elli_handover_tests.erl
@@ -5,12 +5,6 @@ init(Req, _Args) ->
case elli_request:path(Req) of
[<<"hello">>, <<"world">>] ->
{ok, handover};
- [<<"hello">>, <<"maas">>] ->
- % Pass state to handle in handover callback.
- {ok, handover, <<"Maas">>};
- [<<"salute">>] ->
- % Pass state to normal in standard callback
- {ok, standard, <<"Hallo ">>};
_ ->
ignore
end.
@@ -26,18 +20,6 @@ handle('GET', [<<"hello">>, <<"world">>], Req, _Args) ->
{"Content-Length", Size}], Body),
{close, <<>>};
-handle('GET', [<<"hello">>, <<"maas">>], Req, State) ->
- %% Use the passed state.
- Body = <<"Hello ", State/binary, $!>>,
- Size = list_to_binary(integer_to_list(size(Body))),
- elli_http:send_response(Req, 200, [{"Connection", "close"},
- {"Content-Length", Size}], Body),
- {close, <<>>};
-
-handle('GET', [<<"salute">>], Req, State) ->
- %% Fetch a GET argument from the URL, and use the state for a salute.
- Name = elli_request:get_arg(<<"name">>, Req, <<"undefined">>),
- {ok, [], <<State/binary, Name/binary>>};
handle('GET', [<<"hello">>], Req, _Args) ->
%% Fetch a GET argument from the URL.
View
@@ -65,31 +65,31 @@ keepalive_loop(Socket, NumRequests, Buffer, Options, Callback) ->
%% @doc: Handle a HTTP request that will possibly come on the
%% socket. Returns the appropriate connection token and any buffer
%% containing (parts of) the next request.
-handle_request(S, PrevB, Opts, {Mod, _} = Callback) ->
+handle_request(S, PrevB, Opts, {Mod, Args} = Callback) ->
{Method, RawPath, V, B0} = get_request(S, PrevB, Opts, Callback), t(request_start),
{RequestHeaders, B1} = get_headers(S, V, B0, Opts, Callback), t(headers_end),
Req = mk_req(Method, RawPath, RequestHeaders, <<>>, V, S, Callback),
case init(Req) of
- {ok, standard, #req{callback = {Mod, _}=Callback1} = Req1} ->
- {RequestBody, B2} = get_body(S, RequestHeaders, B1, Opts, Callback1), t(body_end),
- Req2 = Req1#req{body = RequestBody},
+ {ok, standard} ->
+ {RequestBody, B2} = get_body(S, RequestHeaders, B1, Opts, Callback), t(body_end),
+ Req1 = Req#req{body = RequestBody},
t(user_start),
- Response = execute_callback(Req2),
+ Response = execute_callback(Req1),
t(user_end),
handle_response(Req1, B2, Response);
- {ok, handover, #req{callback = {Mod, State}} = Req1} ->
- Req2 = Req1#req{body = B1},
+ {ok, handover} ->
+ Req1 = Req#req{body = B1},
t(user_start),
- Response = Mod:handle(Req2, State),
+ Response = Mod:handle(Req1, Args),
t(user_end),
t(request_end),
- handle_event(Mod, request_complete, [Req2, handover, [], <<>>, get_timings()], State),
+ handle_event(Mod, request_complete, [Req1, handover, [], <<>>, get_timings()], Args),
Response
end.
@@ -568,14 +568,12 @@ init(#req{callback = {Mod, Args}} = Req) ->
true ->
case Mod:init(Req, Args) of
ignore ->
- {ok, standard, Req};
+ {ok, standard};
{ok, Behaviour} ->
- {ok, Behaviour, Req};
- {ok, Behaviour, CallbackState} ->
- {ok, Behaviour, Req#req{callback={Mod, CallbackState}}}
+ {ok, Behaviour}
end;
false ->
- {ok, standard, Req}
+ {ok, standard}
end.
handle_event(Mod, Name, EventArgs, ElliArgs) ->
@@ -63,7 +63,7 @@ handle_event(Event, Args, Config) ->
do_init(_, []) ->
{ok, standard};
-do_init(Req, [{Mod, Args} | Mods]) ->
+do_init(Req, [{Mod, Args}|Mods]) ->
case erlang:function_exported(Mod, init, 2) of
true ->
case Mod:init(Req, Args) of
@@ -10,8 +10,6 @@ elli_test_() ->
fun setup/0, fun teardown/1,
[
?_test(hello_world()),
- ?_test(hello_maas()),
- ?_test(salute()),
?_test(echo())
]}.
@@ -42,18 +40,6 @@ hello_world() ->
{"content-length", "12"}], headers(Response)),
?assertEqual("Hello World!", body(Response)).
-hello_maas() ->
- {ok, Response} = httpc:request("http://localhost:3003/hello/maas"),
- ?assertEqual(200, status(Response)),
- ?assertEqual([{"connection", "close"},
- {"content-length", "11"}], headers(Response)),
- ?assertEqual("Hello Maas!", body(Response)).
-
-salute() ->
- {ok, Response} = httpc:request("http://localhost:3003/salute?name=knut"),
- ?assertEqual(200, status(Response)),
- ?assertEqual("Hallo knut", body(Response)).
-
echo() ->
{ok, Response} = httpc:request("http://localhost:3003/hello?name=knut"),
?assertEqual(200, status(Response)),

0 comments on commit 15b7c5a

Please sign in to comment.