diff options
author | Loïc Hoguin <[email protected]> | 2014-09-30 20:12:13 +0300 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2014-09-30 20:12:13 +0300 |
commit | 0dc063ab7d94edb37c61f821b5d8e4c2da7f8ff1 (patch) | |
tree | aaa71b552b0348fc403cc68ba8318e58f213d4fd /test/http_SUITE_data | |
parent | 5ce4c2bfb40ecc4b687a2941e612025a1c4ff913 (diff) | |
download | cowboy-0dc063ab7d94edb37c61f821b5d8e4c2da7f8ff1.tar.gz cowboy-0dc063ab7d94edb37c61f821b5d8e4c2da7f8ff1.tar.bz2 cowboy-0dc063ab7d94edb37c61f821b5d8e4c2da7f8ff1.zip |
Improve handler interface and documentation
This change simplifies a little more the sub protocols mechanism.
Aliases have been removed. The renaming of loop handlers as long
polling handlers has been reverted.
Plain HTTP handlers now simply do their work in the init/2
callback. There is no specific code for them.
Loop handlers now follow the same return value as Websocket,
they use ok to continue and shutdown to stop.
Terminate reasons for all handler types have been documented.
The terminate callback is now appropriately called in all cases
(or should be).
Behaviors for all handler types have been moved in the module
that implement them. This means that cowboy_handler replaces
the cowboy_http_handler behavior, and similarly cowboy_loop
replaces cowboy_loop_handler, cowboy_websocket replaces
cowboy_websocket_handler. Finally cowboy_rest now has the
start of a behavior in it and will have the full list of
optional callbacks defined once Erlang 18.0 gets released.
The guide has been reorganized and should be easier to follow.
Diffstat (limited to 'test/http_SUITE_data')
25 files changed, 33 insertions, 102 deletions
diff --git a/test/http_SUITE_data/http_body_qs.erl b/test/http_SUITE_data/http_body_qs.erl index b219566..945b7fd 100644 --- a/test/http_SUITE_data/http_body_qs.erl +++ b/test/http_SUITE_data/http_body_qs.erl @@ -3,15 +3,11 @@ -module(http_body_qs). -export([init/2]). --export([handle/2]). init(Req, Opts) -> - {http, Req, Opts}. - -handle(Req, State) -> Method = cowboy_req:method(Req), HasBody = cowboy_req:has_body(Req), - {ok, maybe_echo(Method, HasBody, Req), State}. + {ok, maybe_echo(Method, HasBody, Req), Opts}. maybe_echo(<<"POST">>, true, Req) -> case cowboy_req:body_qs(Req) of diff --git a/test/http_SUITE_data/http_chunked.erl b/test/http_SUITE_data/http_chunked.erl index 0c18363..87a6852 100644 --- a/test/http_SUITE_data/http_chunked.erl +++ b/test/http_SUITE_data/http_chunked.erl @@ -3,15 +3,11 @@ -module(http_chunked). -export([init/2]). --export([handle/2]). init(Req, Opts) -> - {http, Req, Opts}. - -handle(Req, State) -> Req2 = cowboy_req:chunked_reply(200, Req), timer:sleep(100), cowboy_req:chunk("chunked_handler\r\n", Req2), timer:sleep(100), cowboy_req:chunk("works fine!", Req2), - {ok, Req2, State}. + {ok, Req2, Opts}. diff --git a/test/http_SUITE_data/http_echo_body.erl b/test/http_SUITE_data/http_echo_body.erl index 8743844..c76b9b3 100644 --- a/test/http_SUITE_data/http_echo_body.erl +++ b/test/http_SUITE_data/http_echo_body.erl @@ -3,18 +3,14 @@ -module(http_echo_body). -export([init/2]). --export([handle/2]). init(Req, Opts) -> - {http, Req, Opts}. - -handle(Req, State) -> true = cowboy_req:has_body(Req), 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}. + {ok, Req3, Opts}. handle_badlength(Req) -> cowboy_req:reply(413, [], <<"Request entity too large">>, Req). diff --git a/test/http_SUITE_data/http_errors.erl b/test/http_SUITE_data/http_errors.erl index 39eda60..f105c58 100644 --- a/test/http_SUITE_data/http_errors.erl +++ b/test/http_SUITE_data/http_errors.erl @@ -3,7 +3,6 @@ -module(http_errors). -export([init/2]). --export([handle/2]). init(Req, _Opts) -> #{'case' := Case} = cowboy_req:match_qs(Req, ['case']), @@ -15,22 +14,4 @@ case_init(<<"init_before_reply">> = Case, _Req) -> case_init(<<"init_after_reply">> = Case, Req) -> cowboy_error_h:ignore(?MODULE, case_init, 2), _ = cowboy_req:reply(200, [], "http_handler_crashes", Req), - error(Case); -case_init(<<"init_reply_handle_error">> = Case, Req) -> - Req1 = cowboy_req:reply(200, [], "http_handler_crashes", Req), - {http, Req1, Case}; -case_init(<<"handle_before_reply">> = Case, Req) -> - {http, Req, Case}; -case_init(<<"handle_after_reply">> = Case, Req) -> - {http, Req, Case}. - -handle(_Req, <<"init_reply_handle_error">> = Case) -> - cowboy_error_h:ignore(?MODULE, handle, 2), - error(Case); -handle(_Req, <<"handle_before_reply">> = Case) -> - cowboy_error_h:ignore(?MODULE, handle, 2), - error(Case); -handle(Req, <<"handle_after_reply">> = Case) -> - cowboy_error_h:ignore(?MODULE, handle, 2), - _ = cowboy_req:reply(200, [], "http_handler_crashes", Req), error(Case). diff --git a/test/http_SUITE_data/http_handler.erl b/test/http_SUITE_data/http_handler.erl index 61c14f8..62e9193 100644 --- a/test/http_SUITE_data/http_handler.erl +++ b/test/http_SUITE_data/http_handler.erl @@ -3,12 +3,8 @@ -module(http_handler). -export([init/2]). --export([handle/2]). init(Req, Opts) -> - {http, Req, Opts}. - -handle(Req, State) -> - Headers = proplists:get_value(headers, State, []), - Body = proplists:get_value(body, State, "http_handler"), - {ok, cowboy_req:reply(200, Headers, Body, Req), State}. + Headers = proplists:get_value(headers, Opts, []), + Body = proplists:get_value(body, Opts, "http_handler"), + {ok, cowboy_req:reply(200, Headers, Body, Req), Opts}. diff --git a/test/http_SUITE_data/http_init_shutdown.erl b/test/http_SUITE_data/http_init_shutdown.erl deleted file mode 100644 index 5aae898..0000000 --- a/test/http_SUITE_data/http_init_shutdown.erl +++ /dev/null @@ -1,10 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(http_init_shutdown). - --export([init/2]). - -init(Req, _) -> - Req2 = cowboy_req:reply(<<"666 Init Shutdown Testing">>, - [{<<"connection">>, <<"close">>}], Req), - {shutdown, Req2, undefined}. diff --git a/test/http_SUITE_data/http_loop_stream_recv.erl b/test/http_SUITE_data/http_loop_stream_recv.erl index 8547cc9..4cd39a2 100644 --- a/test/http_SUITE_data/http_loop_stream_recv.erl +++ b/test/http_SUITE_data/http_loop_stream_recv.erl @@ -9,7 +9,7 @@ init(Req, _) -> receive after 100 -> ok end, self() ! stream, - {long_polling, Req, undefined, 100}. + {cowboy_loop, Req, undefined, 100}. info(stream, Req, undefined) -> stream(Req, 1, <<>>). @@ -17,7 +17,7 @@ info(stream, Req, undefined) -> stream(Req, ID, Acc) -> case cowboy_req:body(Req) of {ok, <<>>, Req2} -> - {ok, cowboy_req:reply(200, Req2), undefined}; + {shutdown, cowboy_req:reply(200, Req2), undefined}; {_, Data, Req2} -> parse_id(Req2, ID, << Acc/binary, Data/binary >>) end. @@ -30,5 +30,5 @@ parse_id(Req, ID, Data) -> stream(Req, ID, Data) end. -terminate({normal, shutdown}, _, _) -> +terminate(shutdown, _, _) -> ok. diff --git a/test/http_SUITE_data/http_multipart.erl b/test/http_SUITE_data/http_multipart.erl index 6bd6408..196cbce 100644 --- a/test/http_SUITE_data/http_multipart.erl +++ b/test/http_SUITE_data/http_multipart.erl @@ -3,14 +3,10 @@ -module(http_multipart). -export([init/2]). --export([handle/2]). init(Req, Opts) -> - {http, Req, Opts}. - -handle(Req, State) -> {Result, Req2} = acc_multipart(Req, []), - {ok, cowboy_req:reply(200, [], term_to_binary(Result), Req2), State}. + {ok, cowboy_req:reply(200, [], term_to_binary(Result), Req2), Opts}. acc_multipart(Req, Acc) -> case cowboy_req:part(Req) of diff --git a/test/http_SUITE_data/http_multipart_stream.erl b/test/http_SUITE_data/http_multipart_stream.erl index 43d459a..82662ad 100644 --- a/test/http_SUITE_data/http_multipart_stream.erl +++ b/test/http_SUITE_data/http_multipart_stream.erl @@ -3,14 +3,10 @@ -module(http_multipart_stream). -export([init/2]). --export([handle/2]). init(Req, Opts) -> - {http, Req, Opts}. - -handle(Req, State) -> Req2 = multipart(Req), - {ok, cowboy_req:reply(200, Req2), State}. + {ok, cowboy_req:reply(200, Req2), Opts}. multipart(Req) -> case cowboy_req:part(Req) of diff --git a/test/http_SUITE_data/http_req_attr.erl b/test/http_SUITE_data/http_req_attr.erl index 9c5acba..ce9c185 100644 --- a/test/http_SUITE_data/http_req_attr.erl +++ b/test/http_SUITE_data/http_req_attr.erl @@ -5,15 +5,11 @@ -module(http_req_attr). -export([init/2]). --export([handle/2]). init(Req, Opts) -> - {http, Req, Opts}. - -handle(Req, State) -> #{attr := Attr} = cowboy_req:match_qs(Req, [attr]), <<"host_and_port">> = Attr, Host = cowboy_req:host(Req), Port = cowboy_req:port(Req), Value = [Host, "\n", integer_to_list(Port)], - {ok, cowboy_req:reply(200, [], Value, Req), State}. + {ok, cowboy_req:reply(200, [], Value, Req), Opts}. diff --git a/test/http_SUITE_data/http_set_resp.erl b/test/http_SUITE_data/http_set_resp.erl index 77196a8..2e7f835 100644 --- a/test/http_SUITE_data/http_set_resp.erl +++ b/test/http_SUITE_data/http_set_resp.erl @@ -3,7 +3,6 @@ -module(http_set_resp). -export([init/2]). --export([handle/2]). init(Req, Opts) -> Headers = proplists:get_value(headers, Opts, []), @@ -14,16 +13,13 @@ init(Req, Opts) -> Req3 = cowboy_req:set_resp_body(Body, Req2), Req4 = cowboy_req:set_resp_header(<<"x-cowboy-test">>, <<"ok">>, Req3), Req5 = cowboy_req:set_resp_cookie(<<"cake">>, <<"lie">>, [], Req4), - {http, Req5, undefined}. - -handle(Req, State) -> - case cowboy_req:has_resp_header(<<"x-cowboy-test">>, Req) of - false -> {ok, Req, State}; + case cowboy_req:has_resp_header(<<"x-cowboy-test">>, Req5) of + false -> {ok, Req5, Opts}; true -> - case cowboy_req:has_resp_body(Req) of + case cowboy_req:has_resp_body(Req5) of false -> - {ok, Req, State}; + {ok, Req5, Opts}; true -> - {ok, cowboy_req:reply(200, Req), State} + {ok, cowboy_req:reply(200, Req5), Opts} end end. diff --git a/test/http_SUITE_data/http_stream_body.erl b/test/http_SUITE_data/http_stream_body.erl index 29569cd..aea5300 100644 --- a/test/http_SUITE_data/http_stream_body.erl +++ b/test/http_SUITE_data/http_stream_body.erl @@ -3,14 +3,10 @@ -module(http_stream_body). -export([init/2]). --export([handle/2]). init(Req, Opts) -> - {http, Req, Opts}. - -handle(Req, State) -> - Body = proplists:get_value(body, State, "http_handler_stream_body"), - Reply = proplists:get_value(reply, State), + Body = proplists:get_value(body, Opts, "http_handler_stream_body"), + Reply = proplists:get_value(reply, Opts), SFun = fun(Socket, Transport) -> Transport:send(Socket, Body) end, Req2 = case Reply of set_resp -> @@ -23,4 +19,4 @@ handle(Req, State) -> SFun2 = fun(SendFun) -> lists:foreach(SendFun, Body) end, cowboy_req:set_resp_body_fun(chunked, SFun2, Req) end, - {ok, cowboy_req:reply(200, Req2), State}. + {ok, cowboy_req:reply(200, Req2), Opts}. diff --git a/test/http_SUITE_data/http_streamed.erl b/test/http_SUITE_data/http_streamed.erl index ba710f1..6ca53fb 100644 --- a/test/http_SUITE_data/http_streamed.erl +++ b/test/http_SUITE_data/http_streamed.erl @@ -3,16 +3,12 @@ -module(http_streamed). -export([init/2]). --export([handle/2]). init(Req, Opts) -> - {http, Req, Opts}. - -handle(Req, State) -> Req2 = cowboy_req:set([{resp_state, waiting_stream}], Req), Req3 = cowboy_req:chunked_reply(200, Req2), timer:sleep(100), cowboy_req:chunk("streamed_handler\r\n", Req3), timer:sleep(100), cowboy_req:chunk("works fine!", Req3), - {ok, Req3, State}. + {ok, Req3, Opts}. diff --git a/test/http_SUITE_data/rest_empty_resource.erl b/test/http_SUITE_data/rest_empty_resource.erl index 97fc26f..8a944cd 100644 --- a/test/http_SUITE_data/rest_empty_resource.erl +++ b/test/http_SUITE_data/rest_empty_resource.erl @@ -3,4 +3,4 @@ -export([init/2]). init(Req, Opts) -> - {rest, Req, Opts}. + {cowboy_rest, Req, Opts}. diff --git a/test/http_SUITE_data/rest_expires.erl b/test/http_SUITE_data/rest_expires.erl index e71b107..8665b06 100644 --- a/test/http_SUITE_data/rest_expires.erl +++ b/test/http_SUITE_data/rest_expires.erl @@ -7,7 +7,7 @@ -export([last_modified/2]). init(Req, Opts) -> - {rest, Req, Opts}. + {cowboy_rest, Req, Opts}. content_types_provided(Req, State) -> {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. diff --git a/test/http_SUITE_data/rest_expires_binary.erl b/test/http_SUITE_data/rest_expires_binary.erl index 84b0675..4d6bd3c 100644 --- a/test/http_SUITE_data/rest_expires_binary.erl +++ b/test/http_SUITE_data/rest_expires_binary.erl @@ -6,7 +6,7 @@ -export([expires/2]). init(Req, Opts) -> - {rest, Req, Opts}. + {cowboy_rest, Req, Opts}. content_types_provided(Req, State) -> {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. diff --git a/test/http_SUITE_data/rest_forbidden_resource.erl b/test/http_SUITE_data/rest_forbidden_resource.erl index 1e6d99f..0a65228 100644 --- a/test/http_SUITE_data/rest_forbidden_resource.erl +++ b/test/http_SUITE_data/rest_forbidden_resource.erl @@ -9,7 +9,7 @@ -export([from_text/2]). init(Req, [Forbidden]) -> - {rest, Req, Forbidden}. + {cowboy_rest, Req, Forbidden}. allowed_methods(Req, State) -> {[<<"GET">>, <<"HEAD">>, <<"POST">>], Req, State}. diff --git a/test/http_SUITE_data/rest_missing_callbacks.erl b/test/http_SUITE_data/rest_missing_callbacks.erl index fec308a..e1fcac1 100644 --- a/test/http_SUITE_data/rest_missing_callbacks.erl +++ b/test/http_SUITE_data/rest_missing_callbacks.erl @@ -6,7 +6,7 @@ -export([content_types_provided/2]). init(Req, Opts) -> - {rest, Req, Opts}. + {cowboy_rest, Req, Opts}. allowed_methods(Req, State) -> {[<<"GET">>, <<"PUT">>], Req, State}. diff --git a/test/http_SUITE_data/rest_nodelete_resource.erl b/test/http_SUITE_data/rest_nodelete_resource.erl index e8123db..b9f40bd 100644 --- a/test/http_SUITE_data/rest_nodelete_resource.erl +++ b/test/http_SUITE_data/rest_nodelete_resource.erl @@ -6,7 +6,7 @@ -export([get_text_plain/2]). init(Req, Opts) -> - {rest, Req, Opts}. + {cowboy_rest, Req, Opts}. allowed_methods(Req, State) -> {[<<"GET">>, <<"HEAD">>, <<"DELETE">>], Req, State}. diff --git a/test/http_SUITE_data/rest_param_all.erl b/test/http_SUITE_data/rest_param_all.erl index 54950eb..2b2ea23 100644 --- a/test/http_SUITE_data/rest_param_all.erl +++ b/test/http_SUITE_data/rest_param_all.erl @@ -8,7 +8,7 @@ -export([put_text_plain/2]). init(Req, Opts) -> - {rest, Req, Opts}. + {cowboy_rest, Req, Opts}. allowed_methods(Req, State) -> {[<<"GET">>, <<"PUT">>], Req, State}. diff --git a/test/http_SUITE_data/rest_patch_resource.erl b/test/http_SUITE_data/rest_patch_resource.erl index 4b81648..03f780f 100644 --- a/test/http_SUITE_data/rest_patch_resource.erl +++ b/test/http_SUITE_data/rest_patch_resource.erl @@ -8,7 +8,7 @@ -export([patch_text_plain/2]). init(Req, Opts) -> - {rest, Req, Opts}. + {cowboy_rest, Req, Opts}. allowed_methods(Req, State) -> {[<<"HEAD">>, <<"GET">>, <<"PATCH">>], Req, State}. diff --git a/test/http_SUITE_data/rest_post_charset_resource.erl b/test/http_SUITE_data/rest_post_charset_resource.erl index 7b7c49c..0be6aa3 100644 --- a/test/http_SUITE_data/rest_post_charset_resource.erl +++ b/test/http_SUITE_data/rest_post_charset_resource.erl @@ -6,7 +6,7 @@ -export([from_text/2]). init(Req, Opts) -> - {rest, Req, Opts}. + {cowboy_rest, Req, Opts}. allowed_methods(Req, State) -> {[<<"POST">>], Req, State}. diff --git a/test/http_SUITE_data/rest_postonly_resource.erl b/test/http_SUITE_data/rest_postonly_resource.erl index 9b14b24..942e55d 100644 --- a/test/http_SUITE_data/rest_postonly_resource.erl +++ b/test/http_SUITE_data/rest_postonly_resource.erl @@ -6,7 +6,7 @@ -export([from_text/2]). init(Req, Opts) -> - {rest, Req, Opts}. + {cowboy_rest, Req, Opts}. allowed_methods(Req, State) -> {[<<"POST">>], Req, State}. diff --git a/test/http_SUITE_data/rest_resource_etags.erl b/test/http_SUITE_data/rest_resource_etags.erl index 9509d0a..23b9dfc 100644 --- a/test/http_SUITE_data/rest_resource_etags.erl +++ b/test/http_SUITE_data/rest_resource_etags.erl @@ -6,7 +6,7 @@ -export([get_text_plain/2]). init(Req, Opts) -> - {rest, Req, Opts}. + {cowboy_rest, Req, Opts}. generate_etag(Req, State) -> #{type := Type} = cowboy_req:match_qs(Req, [type]), diff --git a/test/http_SUITE_data/rest_simple_resource.erl b/test/http_SUITE_data/rest_simple_resource.erl index 3d2787f..68e1b95 100644 --- a/test/http_SUITE_data/rest_simple_resource.erl +++ b/test/http_SUITE_data/rest_simple_resource.erl @@ -5,7 +5,7 @@ -export([get_text_plain/2]). init(Req, Opts) -> - {rest, Req, Opts}. + {cowboy_rest, Req, Opts}. content_types_provided(Req, State) -> {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. |