0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 04:30:25 +00:00

Rename 'halt' to 'stop' for better consistency

Now everywhere in Cowboy when we want to stop something we return
a 'stop' tuple instead of one of the many choices depending on
context that we had before.

This particular change affects middlewares, sub protocols and
REST handlers which were using 'halt' to stop processing.
This commit is contained in:
Loïc Hoguin 2014-11-07 20:19:05 +02:00
parent 8cbd8c1882
commit 999dc5b7c1
13 changed files with 33 additions and 34 deletions

View file

@ -23,7 +23,7 @@ Middlewares can return one of four different values:
* `{ok, Req, Env}` to continue the request processing * `{ok, Req, Env}` to continue the request processing
* `{suspend, Module, Function, Args}` to hibernate * `{suspend, Module, Function, Args}` to hibernate
* `{halt, Req}` to stop processing and move on to the next request * `{stop, Req}` to stop processing and move on to the next request
Of note is that when hibernating, processing will resume on the given Of note is that when hibernating, processing will resume on the given
MFA, discarding all previous stacktrace. Make sure you keep the `Req` MFA, discarding all previous stacktrace. Make sure you keep the `Req`

View file

@ -41,7 +41,7 @@ you need.
All callbacks take two arguments, the Req object and the State, All callbacks take two arguments, the Req object and the State,
and return a three-element tuple of the form `{Value, Req, State}`. and return a three-element tuple of the form `{Value, Req, State}`.
All callbacks can also return `{halt, Req, State}` to stop execution All callbacks can also return `{stop, Req, State}` to stop execution
of the request. of the request.
The following table summarizes the callbacks and their default values. The following table summarizes the callbacks and their default values.

View file

@ -21,7 +21,7 @@ optionally with its contents modified.
: execute(Req, Env) : execute(Req, Env)
-> {ok, Req, Env} -> {ok, Req, Env}
| {suspend, Module, Function, Args} | {suspend, Module, Function, Args}
| {halt, Req} | {stop, Req}
Types: Types:
@ -41,7 +41,7 @@ The `suspend` return value will hibernate the process until
an Erlang message is received. Note that when resuming, any an Erlang message is received. Note that when resuming, any
previous stacktrace information will be gone. previous stacktrace information will be gone.
The `halt` return value stops Cowboy from doing any further The `stop` return value stops Cowboy from doing any further
processing of the request, even if there are middlewares processing of the request, even if there are middlewares
that haven't been executed yet. The connection may be left that haven't been executed yet. The connection may be left
open to receive more requests from the client. open to receive more requests from the client.

View file

@ -58,7 +58,7 @@ stacktrace of the process when the crash occurred.
:: Callbacks :: Callbacks
: Callback(Req, State) -> {Value, Req, State} | {halt, Req, State} : Callback(Req, State) -> {Value, Req, State} | {stop, Req, State}
Types: Types:
@ -72,7 +72,7 @@ on the `Value` type, the default value if the callback is
not defined, and more general information on when the not defined, and more general information on when the
callback is called and what its intended use is. callback is called and what its intended use is.
The `halt` tuple can be returned to stop REST processing. The `stop` tuple can be returned to stop REST processing.
It is up to the resource code to send a reply before that, It is up to the resource code to send a reply before that,
otherwise a `204 No Content` will be sent. otherwise a `204 No Content` will be sent.

View file

@ -8,8 +8,7 @@ by modules that implement a protocol on top of HTTP.
: upgrade(Req, Env, Handler, Opts) : upgrade(Req, Env, Handler, Opts)
-> {ok, Req, Env} -> {ok, Req, Env}
| {suspend, Module, Function, Args} | {suspend, Module, Function, Args}
| {halt, Req} | {stop, Req}
| {error, StatusCode, Req}
Types: Types:

View file

@ -20,5 +20,5 @@
-callback execute(Req, Env) -callback execute(Req, Env)
-> {ok, Req, Env} -> {ok, Req, Env}
| {suspend, module(), atom(), [any()]} | {suspend, module(), atom(), [any()]}
| {halt, Req} | {stop, Req}
when Req::cowboy_req:req(), Env::env(). when Req::cowboy_req:req(), Env::env().

View file

@ -431,7 +431,7 @@ execute(Req, State, Env, [Middleware|Tail]) ->
{suspend, Module, Function, Args} -> {suspend, Module, Function, Args} ->
erlang:hibernate(?MODULE, resume, erlang:hibernate(?MODULE, resume,
[State, Env, Tail, Module, Function, Args]); [State, Env, Tail, Module, Function, Args]);
{halt, Req2} -> {stop, Req2} ->
next_request(Req2, State, ok) next_request(Req2, State, ok)
end. end.
@ -444,7 +444,7 @@ resume(State, Env, Tail, Module, Function, Args) ->
{suspend, Module2, Function2, Args2} -> {suspend, Module2, Function2, Args2} ->
erlang:hibernate(?MODULE, resume, erlang:hibernate(?MODULE, resume,
[State, Env, Tail, Module2, Function2, Args2]); [State, Env, Tail, Module2, Function2, Args2]);
{halt, Req2} -> {stop, Req2} ->
next_request(Req2, State, ok) next_request(Req2, State, ok)
end. end.

View file

@ -84,7 +84,7 @@ known_methods(Req, State=#state{method=Method}) ->
next(Req, State, fun uri_too_long/2); next(Req, State, fun uri_too_long/2);
no_call -> no_call ->
next(Req, State, 501); next(Req, State, 501);
{halt, Req2, HandlerState} -> {stop, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState}); terminate(Req2, State#state{handler_state=HandlerState});
{List, Req2, HandlerState} -> {List, Req2, HandlerState} ->
State2 = State#state{handler_state=HandlerState}, State2 = State#state{handler_state=HandlerState},
@ -109,7 +109,7 @@ allowed_methods(Req, State=#state{method=Method}) ->
no_call -> no_call ->
method_not_allowed(Req, State, method_not_allowed(Req, State,
[<<"HEAD">>, <<"GET">>, <<"OPTIONS">>]); [<<"HEAD">>, <<"GET">>, <<"OPTIONS">>]);
{halt, Req2, HandlerState} -> {stop, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState}); terminate(Req2, State#state{handler_state=HandlerState});
{List, Req2, HandlerState} -> {List, Req2, HandlerState} ->
State2 = State#state{handler_state=HandlerState}, State2 = State#state{handler_state=HandlerState},
@ -140,7 +140,7 @@ is_authorized(Req, State) ->
case call(Req, State, is_authorized) of case call(Req, State, is_authorized) of
no_call -> no_call ->
forbidden(Req, State); forbidden(Req, State);
{halt, Req2, HandlerState} -> {stop, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState}); terminate(Req2, State#state{handler_state=HandlerState});
{true, Req2, HandlerState} -> {true, Req2, HandlerState} ->
forbidden(Req2, State#state{handler_state=HandlerState}); forbidden(Req2, State#state{handler_state=HandlerState});
@ -172,7 +172,7 @@ options(Req, State=#state{allowed_methods=Methods, method= <<"OPTIONS">>}) ->
= << << ", ", M/binary >> || M <- Methods >>, = << << ", ", M/binary >> || M <- Methods >>,
Req2 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req), Req2 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req),
respond(Req2, State, 200); respond(Req2, State, 200);
{halt, Req2, HandlerState} -> {stop, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState}); terminate(Req2, State#state{handler_state=HandlerState});
{ok, Req2, HandlerState} -> {ok, Req2, HandlerState} ->
respond(Req2, State#state{handler_state=HandlerState}, 200) respond(Req2, State#state{handler_state=HandlerState}, 200)
@ -211,7 +211,7 @@ content_types_provided(Req, State) ->
catch _:_ -> catch _:_ ->
respond(Req, State2, 400) respond(Req, State2, 400)
end; end;
{halt, Req2, HandlerState} -> {stop, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState}); terminate(Req2, State#state{handler_state=HandlerState});
{[], Req2, HandlerState} -> {[], Req2, HandlerState} ->
not_acceptable(Req2, State#state{handler_state=HandlerState}); not_acceptable(Req2, State#state{handler_state=HandlerState});
@ -313,7 +313,7 @@ languages_provided(Req, State) ->
case call(Req, State, languages_provided) of case call(Req, State, languages_provided) of
no_call -> no_call ->
charsets_provided(Req, State); charsets_provided(Req, State);
{halt, Req2, HandlerState} -> {stop, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState}); terminate(Req2, State#state{handler_state=HandlerState});
{[], Req2, HandlerState} -> {[], Req2, HandlerState} ->
not_acceptable(Req2, State#state{handler_state=HandlerState}); not_acceptable(Req2, State#state{handler_state=HandlerState});
@ -373,7 +373,7 @@ charsets_provided(Req, State) ->
case call(Req, State, charsets_provided) of case call(Req, State, charsets_provided) of
no_call -> no_call ->
set_content_type(Req, State); set_content_type(Req, State);
{halt, Req2, HandlerState} -> {stop, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState}); terminate(Req2, State#state{handler_state=HandlerState});
{[], Req2, HandlerState} -> {[], Req2, HandlerState} ->
not_acceptable(Req2, State#state{handler_state=HandlerState}); not_acceptable(Req2, State#state{handler_state=HandlerState});
@ -645,7 +645,7 @@ moved_permanently(Req, State, OnFalse) ->
respond(Req3, State#state{handler_state=HandlerState}, 301); respond(Req3, State#state{handler_state=HandlerState}, 301);
{false, Req2, HandlerState} -> {false, Req2, HandlerState} ->
OnFalse(Req2, State#state{handler_state=HandlerState}); OnFalse(Req2, State#state{handler_state=HandlerState});
{halt, Req2, HandlerState} -> {stop, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState}); terminate(Req2, State#state{handler_state=HandlerState});
no_call -> no_call ->
OnFalse(Req, State) OnFalse(Req, State)
@ -666,7 +666,7 @@ moved_temporarily(Req, State) ->
respond(Req3, State#state{handler_state=HandlerState}, 307); respond(Req3, State#state{handler_state=HandlerState}, 307);
{false, Req2, HandlerState} -> {false, Req2, HandlerState} ->
is_post_to_missing_resource(Req2, State#state{handler_state=HandlerState}, 410); is_post_to_missing_resource(Req2, State#state{handler_state=HandlerState}, 410);
{halt, Req2, HandlerState} -> {stop, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState}); terminate(Req2, State#state{handler_state=HandlerState});
no_call -> no_call ->
is_post_to_missing_resource(Req, State, 410) is_post_to_missing_resource(Req, State, 410)
@ -716,7 +716,7 @@ accept_resource(Req, State) ->
case call(Req, State, content_types_accepted) of case call(Req, State, content_types_accepted) of
no_call -> no_call ->
respond(Req, State, 415); respond(Req, State, 415);
{halt, Req2, HandlerState} -> {stop, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState}); terminate(Req2, State#state{handler_state=HandlerState});
{CTA, Req2, HandlerState} -> {CTA, Req2, HandlerState} ->
CTA2 = [normalize_content_types(P) || P <- CTA], CTA2 = [normalize_content_types(P) || P <- CTA],
@ -751,7 +751,7 @@ choose_content_type(Req, State, ContentType, [_Any|Tail]) ->
process_content_type(Req, State=#state{method=Method, exists=Exists}, Fun) -> process_content_type(Req, State=#state{method=Method, exists=Exists}, Fun) ->
try case call(Req, State, Fun) of try case call(Req, State, Fun) of
{halt, Req2, HandlerState2} -> {stop, Req2, HandlerState2} ->
terminate(Req2, State#state{handler_state=HandlerState2}); terminate(Req2, State#state{handler_state=HandlerState2});
{true, Req2, HandlerState2} when Exists -> {true, Req2, HandlerState2} when Exists ->
State2 = State#state{handler_state=HandlerState2}, State2 = State#state{handler_state=HandlerState2},
@ -832,7 +832,7 @@ set_resp_body_expires(Req, State) ->
%% it to the response. %% it to the response.
set_resp_body(Req, State=#state{content_type_a={_, Callback}}) -> set_resp_body(Req, State=#state{content_type_a={_, Callback}}) ->
try case call(Req, State, Callback) of try case call(Req, State, Callback) of
{halt, Req2, HandlerState2} -> {stop, Req2, HandlerState2} ->
terminate(Req2, State#state{handler_state=HandlerState2}); terminate(Req2, State#state{handler_state=HandlerState2});
{Body, Req2, HandlerState2} -> {Body, Req2, HandlerState2} ->
State2 = State#state{handler_state=HandlerState2}, State2 = State#state{handler_state=HandlerState2},
@ -936,7 +936,7 @@ expect(Req, State, Callback, Expected, OnTrue, OnFalse) ->
case call(Req, State, Callback) of case call(Req, State, Callback) of
no_call -> no_call ->
next(Req, State, OnTrue); next(Req, State, OnTrue);
{halt, Req2, HandlerState} -> {stop, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState}); terminate(Req2, State#state{handler_state=HandlerState});
{Expected, Req2, HandlerState} -> {Expected, Req2, HandlerState} ->
next(Req2, State#state{handler_state=HandlerState}, OnTrue); next(Req2, State#state{handler_state=HandlerState}, OnTrue);

View file

@ -157,7 +157,7 @@ compile_brackets_split(<< C, Rest/bits >>, Acc, N) ->
compile_brackets_split(Rest, << Acc/binary, C >>, N). compile_brackets_split(Rest, << Acc/binary, C >>, N).
-spec execute(Req, Env) -spec execute(Req, Env)
-> {ok, Req, Env} | {halt, Req} -> {ok, Req, Env} | {stop, Req}
when Req::cowboy_req:req(), Env::cowboy_middleware:env(). when Req::cowboy_req:req(), Env::cowboy_middleware:env().
execute(Req, Env) -> execute(Req, Env) ->
{_, Dispatch} = lists:keyfind(dispatch, 1, Env), {_, Dispatch} = lists:keyfind(dispatch, 1, Env),
@ -168,11 +168,11 @@ execute(Req, Env) ->
Req2 = cowboy_req:set_bindings(HostInfo, PathInfo, Bindings, Req), Req2 = cowboy_req:set_bindings(HostInfo, PathInfo, Bindings, Req),
{ok, Req2, [{handler, Handler}, {handler_opts, HandlerOpts}|Env]}; {ok, Req2, [{handler, Handler}, {handler_opts, HandlerOpts}|Env]};
{error, notfound, host} -> {error, notfound, host} ->
{halt, cowboy_req:reply(400, Req)}; {stop, cowboy_req:reply(400, Req)};
{error, badrequest, path} -> {error, badrequest, path} ->
{halt, cowboy_req:reply(400, Req)}; {stop, cowboy_req:reply(400, Req)};
{error, notfound, path} -> {error, notfound, path} ->
{halt, cowboy_req:reply(404, Req)} {stop, cowboy_req:reply(404, Req)}
end. end.
%% Internal. %% Internal.

View file

@ -406,7 +406,7 @@ execute(Req, Env, [Middleware|Tail]) ->
{suspend, Module, Function, Args} -> {suspend, Module, Function, Args} ->
erlang:hibernate(?MODULE, resume, erlang:hibernate(?MODULE, resume,
[Env, Tail, Module, Function, Args]); [Env, Tail, Module, Function, Args]);
{halt, Req2} -> {stop, Req2} ->
cowboy_req:ensure_response(Req2, 204) cowboy_req:ensure_response(Req2, 204)
end. end.
@ -419,7 +419,7 @@ resume(Env, Tail, Module, Function, Args) ->
{suspend, Module2, Function2, Args2} -> {suspend, Module2, Function2, Args2} ->
erlang:hibernate(?MODULE, resume, erlang:hibernate(?MODULE, resume,
[Env, Tail, Module2, Function2, Args2]); [Env, Tail, Module2, Function2, Args2]);
{halt, Req2} -> {stop, Req2} ->
cowboy_req:ensure_response(Req2, 204) cowboy_req:ensure_response(Req2, 204)
end. end.

View file

@ -16,5 +16,5 @@
-module(cowboy_sub_protocol). -module(cowboy_sub_protocol).
-callback upgrade(Req, Env, module(), any(), timeout(), run | hibernate) -callback upgrade(Req, Env, module(), any(), timeout(), run | hibernate)
-> {ok, Req, Env} | {suspend, module(), atom(), [any()]} | {halt, Req} -> {ok, Req, Env} | {suspend, module(), atom(), [any()]} | {stop, Req}
when Req::cowboy_req:req(), Env::cowboy_middleware:env(). when Req::cowboy_req:req(), Env::cowboy_middleware:env().

View file

@ -706,7 +706,7 @@ rest_patch(Config) ->
Tests = [ Tests = [
{204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>}, {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
{400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>}, {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
{400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>}, {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
{415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>} {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
], ],
ConnPid = gun_open(Config), ConnPid = gun_open(Config),

View file

@ -29,8 +29,8 @@ content_types_accepted(Req, State) ->
patch_text_plain(Req, State) -> patch_text_plain(Req, State) ->
case cowboy_req:body(Req) of case cowboy_req:body(Req) of
{ok, <<"halt">>, Req0} -> {ok, <<"stop">>, Req0} ->
{halt, cowboy_req:reply(400, Req0), State}; {stop, cowboy_req:reply(400, Req0), State};
{ok, <<"false">>, Req0} -> {ok, <<"false">>, Req0} ->
{false, Req0, State}; {false, Req0, State};
{ok, _Body, Req0} -> {ok, _Body, Req0} ->