0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 20:50:24 +00:00

Breaking update of the cowboy_req interface

Simplify the interface for most cowboy_req functions. They all return
a single value except the four body reading functions. The reply functions
now only return a Req value.

Access functions do not return a Req anymore.

Functions that used to cache results do not have a cache anymore.

The interface for accessing query string and cookies has therefore
been changed.

There are now three query string functions: qs/1 provides access
to the raw query string value; parse_qs/1 returns the query string
as a list of key/values; match_qs/2 returns a map containing the
values requested in the second argument, after applying constraints
and default value.

Similarly, there are two cookie functions: parse_cookies/1 and
match_cookies/2. More match functions will be added in future commits.

None of the functions return an error tuple anymore. It either works
or crashes. Cowboy will attempt to provide an appropriate status code
in the response of crashed handlers.

As a result, the content decode function has its return value changed
to a simple binary, and the body reading functions only return on success.
This commit is contained in:
Loïc Hoguin 2014-09-23 16:43:29 +03:00
parent b57f94661f
commit f1c3b6d76f
61 changed files with 814 additions and 767 deletions

View file

@ -15,8 +15,7 @@ init(_, Req, _) ->
{loop, Req, 2, 5000, hibernate}.
info(timeout, Req, 0) ->
{ok, Req2} = cowboy_req:reply(102, Req),
{ok, Req2, 0};
{ok, cowboy_req:reply(102, Req), 0};
info(timeout, Req, Count) ->
erlang:send_after(200, self(), timeout),
{loop, Req, Count - 1, hibernate}.

View file

@ -17,8 +17,7 @@ init(_, Req, _) ->
info(timeout, Req, State) ->
{ok, Body, Req2} = cowboy_req:body(Req),
100000 = byte_size(Body),
{ok, Req3} = cowboy_req:reply(200, Req2),
{ok, Req3, State}.
{ok, cowboy_req:reply(200, Req2), State}.
terminate({normal, shutdown}, _, _) ->
ok.

View file

@ -16,8 +16,7 @@ init(_, Req, _) ->
{loop, Req, undefined, 200, hibernate}.
info(timeout, Req, State) ->
{ok, Req2} = cowboy_req:reply(500, Req),
{ok, Req2, State}.
{ok, cowboy_req:reply(500, Req), State}.
terminate({normal, timeout}, _, _) ->
ok.

View file

@ -592,13 +592,11 @@ onrequest_reply(Config) ->
%% Hook for the above onrequest tests.
do_onrequest_hook(Req) ->
case cowboy_req:qs_val(<<"reply">>, Req) of
{undefined, Req2} ->
cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2);
{_, Req2} ->
{ok, Req3} = cowboy_req:reply(
200, [], <<"replied!">>, Req2),
Req3
case cowboy_req:match_qs(Req, [{reply, [], noreply}]) of
#{reply := noreply} ->
cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req);
_ ->
cowboy_req:reply(200, [], <<"replied!">>, Req)
end.
onresponse_capitalize(Config) ->
@ -612,8 +610,7 @@ onresponse_capitalize(Config) ->
do_onresponse_capitalize_hook(Status, Headers, Body, Req) ->
Headers2 = [{cowboy_bstr:capitalize_token(N), V}
|| {N, V} <- Headers],
{ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req),
Req2.
cowboy_req:reply(Status, Headers2, Body, Req).
onresponse_crash(Config) ->
ConnPid = gun_open(Config),
@ -630,9 +627,7 @@ onresponse_reply(Config) ->
%% Hook for the above onresponse tests.
do_onresponse_hook(_, Headers, _, Req) ->
{ok, Req2} = cowboy_req:reply(
<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
Req2.
cowboy_req:reply(<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req).
parse_host(Config) ->
ConnPid = gun_open(Config),

View file

@ -8,10 +8,9 @@ init({_, http}, Req, _) ->
{ok, Req, undefined}.
handle(Req, State) ->
{Method, Req2} = cowboy_req:method(Req),
HasBody = cowboy_req:has_body(Req2),
{ok, Req3} = maybe_echo(Method, HasBody, Req2),
{ok, Req3, State}.
Method = cowboy_req:method(Req),
HasBody = cowboy_req:has_body(Req),
{ok, maybe_echo(Method, HasBody, Req), State}.
maybe_echo(<<"POST">>, true, Req) ->
case cowboy_req:body_qs(Req) of
@ -20,7 +19,6 @@ maybe_echo(<<"POST">>, true, Req) ->
{ok, PostVals, Req2} ->
echo(proplists:get_value(<<"echo">>, PostVals), Req2)
end;
maybe_echo(<<"POST">>, false, Req) ->
cowboy_req:reply(400, [], <<"Missing body.">>, Req);
maybe_echo(_, _, Req) ->

View file

@ -8,7 +8,7 @@ init({_Transport, http}, Req, _Opts) ->
{ok, Req, undefined}.
handle(Req, State) ->
{ok, Req2} = cowboy_req:chunked_reply(200, Req),
Req2 = cowboy_req:chunked_reply(200, Req),
timer:sleep(100),
cowboy_req:chunk("chunked_handler\r\n", Req2),
timer:sleep(100),

View file

@ -9,21 +9,19 @@ init({_, http}, Req, _) ->
handle(Req, State) ->
true = cowboy_req:has_body(Req),
{ok, Req3} = case cowboy_req:body(Req, [{length, 1000000}]) of
Req3 = case cowboy_req:body(Req, [{length, 1000000}]) of
{ok, Body, Req2} -> handle_body(Req2, Body);
{more, _, Req2} -> handle_badlength(Req2)
end,
{ok, Req3, State}.
handle_badlength(Req) ->
{ok, Req2} = cowboy_req:reply(413, [], <<"Request entity too large">>, Req),
{ok, Req2}.
cowboy_req:reply(413, [], <<"Request entity too large">>, Req).
handle_body(Req, Body) ->
{Size, Req2} = cowboy_req:body_length(Req),
Size = cowboy_req:body_length(Req),
Size = byte_size(Body),
{ok, Req3} = cowboy_req:reply(200, [], Body, Req2),
{ok, Req3}.
cowboy_req:reply(200, [], Body, Req).
terminate(_, _, _) ->
ok.

View file

@ -5,18 +5,18 @@
-export([init/3, handle/2, terminate/3]).
init({_Transport, http}, Req, _Opts) ->
{Case, Req1} = cowboy_req:qs_val(<<"case">>, Req),
case_init(Case, Req1).
#{'case' := Case} = cowboy_req:match_qs(Req, ['case']),
case_init(Case, Req).
case_init(<<"init_before_reply">> = Case, _Req) ->
cowboy_error_h:ignore(?MODULE, case_init, 2),
erlang:error(Case);
case_init(<<"init_after_reply">> = Case, Req) ->
cowboy_error_h:ignore(?MODULE, case_init, 2),
{ok, _Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req),
_ = cowboy_req:reply(200, [], "http_handler_crashes", Req),
erlang:error(Case);
case_init(<<"init_reply_handle_error">> = Case, Req) ->
{ok, Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req),
Req1 = cowboy_req:reply(200, [], "http_handler_crashes", Req),
{ok, Req1, Case};
case_init(<<"handle_before_reply">> = Case, Req) ->
{ok, Req, Case};
@ -31,7 +31,7 @@ handle(_Req, <<"handle_before_reply">> = Case) ->
erlang:error(Case);
handle(Req, <<"handle_after_reply">> = Case) ->
cowboy_error_h:ignore(?MODULE, handle, 2),
{ok, _Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req),
_ = cowboy_req:reply(200, [], "http_handler_crashes", Req),
erlang:error(Case).
terminate(_, _, _) ->

View file

@ -12,8 +12,7 @@ init({_Transport, http}, Req, Opts) ->
{ok, Req, #state{headers=Headers, body=Body}}.
handle(Req, State=#state{headers=Headers, body=Body}) ->
{ok, Req2} = cowboy_req:reply(200, Headers, Body, Req),
{ok, Req2, State}.
{ok, cowboy_req:reply(200, Headers, Body, Req), State}.
terminate(_, _, _) ->
ok.

View file

@ -5,12 +5,12 @@
-export([init/3, handle/2, terminate/3]).
init({_Transport, http}, Req, _Opts) ->
{ok, Req2} = cowboy_req:reply(<<"666 Init Shutdown Testing">>,
Req2 = cowboy_req:reply(<<"666 Init Shutdown Testing">>,
[{<<"connection">>, <<"close">>}], Req),
{shutdown, Req2, undefined}.
handle(Req, State) ->
{ok, Req2} = cowboy_req:reply(200, [], "Hello world!", Req),
Req2 = cowboy_req:reply(200, [], "Hello world!", Req),
{ok, Req2, State}.
terminate(_, _, _) ->

View file

@ -16,8 +16,7 @@ info(stream, Req, undefined) ->
stream(Req, ID, Acc) ->
case cowboy_req:body(Req) of
{ok, <<>>, Req2} ->
{ok, Req3} = cowboy_req:reply(200, Req2),
{ok, Req3, undefined};
{ok, cowboy_req:reply(200, Req2), undefined};
{_, Data, Req2} ->
parse_id(Req2, ID, << Acc/binary, Data/binary >>)
end.

View file

@ -9,8 +9,7 @@ init({_Transport, http}, Req, []) ->
handle(Req, State) ->
{Result, Req2} = acc_multipart(Req, []),
{ok, Req3} = cowboy_req:reply(200, [], term_to_binary(Result), Req2),
{ok, Req3, State}.
{ok, cowboy_req:reply(200, [], term_to_binary(Result), Req2), State}.
terminate(_, _, _) ->
ok.

View file

@ -9,8 +9,7 @@ init(_, Req, []) ->
handle(Req, State) ->
Req2 = multipart(Req),
{ok, Req3} = cowboy_req:reply(200, Req2),
{ok, Req3, State}.
{ok, cowboy_req:reply(200, Req2), State}.
terminate(_, _, _) ->
ok.

View file

@ -5,15 +5,14 @@
-export([init/3, handle/2, terminate/3]).
init({_, http}, Req, _) ->
{Attr, Req2} = cowboy_req:qs_val(<<"attr">>, Req),
{ok, Req2, Attr}.
#{attr := Attr} = cowboy_req:match_qs(Req, [attr]),
{ok, Req, Attr}.
handle(Req, <<"host_and_port">> = Attr) ->
{Host, Req2} = cowboy_req:host(Req),
{Port, Req3} = cowboy_req:port(Req2),
Host = cowboy_req:host(Req),
Port = cowboy_req:port(Req),
Value = [Host, "\n", integer_to_list(Port)],
{ok, Req4} = cowboy_req:reply(200, [], Value, Req3),
{ok, Req4, Attr}.
{ok, cowboy_req:reply(200, [], Value, Req), Attr}.
terminate(_, _, _) ->
ok.

View file

@ -20,10 +20,10 @@ handle(Req, State) ->
false -> {ok, Req, State};
true ->
case cowboy_req:has_resp_body(Req) of
false -> {ok, Req, State};
false ->
{ok, Req, State};
true ->
{ok, Req2} = cowboy_req:reply(200, Req),
{ok, Req2, State}
{ok, cowboy_req:reply(200, Req), State}
end
end.

View file

@ -25,8 +25,7 @@ handle(Req, State=#state{headers=_Headers, body=Body, reply=Reply}) ->
SFun2 = fun(SendFun) -> lists:foreach(SendFun, Body) end,
cowboy_req:set_resp_body_fun(chunked, SFun2, Req)
end,
{ok, Req3} = cowboy_req:reply(200, Req2),
{ok, Req3, State}.
{ok, cowboy_req:reply(200, Req2), State}.
terminate(_, _, _) ->
ok.

View file

@ -9,7 +9,7 @@ init({_Transport, http}, Req, _Opts) ->
handle(Req, State) ->
Req2 = cowboy_req:set([{resp_state, waiting_stream}], Req),
{ok, Req3} = cowboy_req:chunked_reply(200, Req2),
Req3 = cowboy_req:chunked_reply(200, Req2),
timer:sleep(100),
cowboy_req:chunk("streamed_handler\r\n", Req3),
timer:sleep(100),

View file

@ -27,5 +27,4 @@ to_text(Req, State) ->
{<<"This is REST!">>, Req, State}.
from_text(Req, State) ->
{Path, Req2} = cowboy_req:path(Req),
{{true, Path}, Req2, State}.
{{true, cowboy_req:path(Req)}, Req, State}.

View file

@ -17,17 +17,16 @@ content_types_provided(Req, State) ->
{[{{<<"text">>, <<"plain">>, '*'}, get_text_plain}], Req, State}.
get_text_plain(Req, State) ->
{{_, _, Param}, Req2} =
cowboy_req:meta(media_type, Req, {{<<"text">>, <<"plain">>}, []}),
{_, _, Param} = cowboy_req:meta(media_type, Req, {{<<"text">>, <<"plain">>}, []}),
Body = if
Param == '*' ->
<<"'*'">>;
Param == [] ->
<<"[]">>;
Param /= [] ->
iolist_to_binary([[Key, $=, Value] || {Key, Value} <- Param])
Param == '*' ->
<<"'*'">>;
Param == [] ->
<<"[]">>;
Param /= [] ->
iolist_to_binary([[Key, $=, Value] || {Key, Value} <- Param])
end,
{Body, Req2, State}.
{Body, Req, State}.
content_types_accepted(Req, State) ->
{[{{<<"text">>, <<"plain">>, '*'}, put_text_plain}], Req, State}.

View file

@ -16,17 +16,16 @@ get_text_plain(Req, State) ->
content_types_accepted(Req, State) ->
case cowboy_req:method(Req) of
{<<"PATCH">>, Req0} ->
{[{{<<"text">>, <<"plain">>, []}, patch_text_plain}], Req0, State};
{_, Req0} ->
{[], Req0, State}
<<"PATCH">> ->
{[{{<<"text">>, <<"plain">>, []}, patch_text_plain}], Req, State};
_ ->
{[], Req, State}
end.
patch_text_plain(Req, State) ->
case cowboy_req:body(Req) of
{ok, <<"halt">>, Req0} ->
{ok, Req1} = cowboy_req:reply(400, Req0),
{halt, Req1, State};
{halt, cowboy_req:reply(400, Req0), State};
{ok, <<"false">>, Req0} ->
{false, Req0, State};
{ok, _Body, Req0} ->

View file

@ -5,24 +5,25 @@ init(_Transport, _Req, _Opts) ->
{upgrade, protocol, cowboy_rest}.
generate_etag(Req, State) ->
case cowboy_req:qs_val(<<"type">>, Req) of
#{type := Type} = cowboy_req:match_qs(Req, [type]),
case Type of
%% Correct return values from generate_etag/2.
{<<"tuple-weak">>, Req2} ->
{{weak, <<"etag-header-value">>}, Req2, State};
{<<"tuple-strong">>, Req2} ->
{{strong, <<"etag-header-value">>}, Req2, State};
<<"tuple-weak">> ->
{{weak, <<"etag-header-value">>}, Req, State};
<<"tuple-strong">> ->
{{strong, <<"etag-header-value">>}, Req, State};
%% Backwards compatible return values from generate_etag/2.
{<<"binary-weak-quoted">>, Req2} ->
{<<"W/\"etag-header-value\"">>, Req2, State};
{<<"binary-strong-quoted">>, Req2} ->
{<<"\"etag-header-value\"">>, Req2, State};
<<"binary-weak-quoted">> ->
{<<"W/\"etag-header-value\"">>, Req, State};
<<"binary-strong-quoted">> ->
{<<"\"etag-header-value\"">>, Req, State};
%% Invalid return values from generate_etag/2.
{<<"binary-strong-unquoted">>, Req2} ->
<<"binary-strong-unquoted">> ->
cowboy_error_h:ignore(cowboy_http, quoted_string, 2),
{<<"etag-header-value">>, Req2, State};
{<<"binary-weak-unquoted">>, Req2} ->
{<<"etag-header-value">>, Req, State};
<<"binary-weak-unquoted">> ->
cowboy_error_h:ignore(cowboy_http, quoted_string, 2),
{<<"W/etag-header-value">>, Req2, State}
{<<"W/etag-header-value">>, Req, State}
end.
content_types_provided(Req, State) ->

View file

@ -10,8 +10,7 @@ init(_Any, _Req, _Opts) ->
{upgrade, protocol, cowboy_websocket}.
websocket_init(_TransportName, Req, _Opts) ->
{ok, Req2} = cowboy_req:reply(403, Req),
{shutdown, Req2}.
{shutdown, cowboy_req:reply(403, Req)}.
websocket_handle(_Frame, _Req, _State) ->
exit(badarg).