From 5ce4c2bfb40ecc4b687a2941e612025a1c4ff913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 26 Sep 2014 15:58:44 +0300 Subject: Unify the init and terminate callbacks This set of changes is the first step to simplify the writing of handlers, by removing some extraneous callbacks and making others optional. init/3 is now init/2, its first argument being removed. rest_init/2 and rest_terminate/2 have been removed. websocket_init/3 and websocket_terminate/3 have been removed. terminate/3 is now optional. It is called regardless of the type of handler, including rest and websocket. The return value of init/2 changed. It now returns {Mod, Req, Opts} with Mod being either one of the four handler type or a custom module. It can also return extra timeout and hibernate options. The signature for sub protocols has changed, they now receive these extra timeout and hibernate options. Loop handlers are now implemented in cowboy_long_polling, and will be renamed throughout the project in a future commit. --- test/handlers/input_crash_h.erl | 4 +-- test/handlers/long_polling_h.erl | 7 ++--- test/handlers/loop_handler_body_h.erl | 7 ++--- test/handlers/loop_handler_timeout_h.erl | 7 ++--- test/http_SUITE_data/http_body_qs.erl | 12 ++++---- test/http_SUITE_data/http_chunked.erl | 12 ++++---- test/http_SUITE_data/http_echo_body.erl | 12 ++++---- test/http_SUITE_data/http_errors.erl | 16 +++++------ test/http_SUITE_data/http_handler.erl | 18 +++++------- test/http_SUITE_data/http_init_shutdown.erl | 13 ++------- test/http_SUITE_data/http_loop_stream_recv.erl | 7 +++-- test/http_SUITE_data/http_multipart.erl | 12 ++++---- test/http_SUITE_data/http_multipart_stream.erl | 12 ++++---- test/http_SUITE_data/http_req_attr.erl | 21 +++++++------- test/http_SUITE_data/http_set_resp.erl | 12 ++++---- test/http_SUITE_data/http_stream_body.erl | 19 +++++-------- test/http_SUITE_data/http_streamed.erl | 12 ++++---- test/http_SUITE_data/rest_empty_resource.erl | 7 +++-- test/http_SUITE_data/rest_expires.erl | 6 ++-- test/http_SUITE_data/rest_expires_binary.erl | 6 ++-- test/http_SUITE_data/rest_forbidden_resource.erl | 16 ++++++----- test/http_SUITE_data/rest_missing_callbacks.erl | 7 +++-- test/http_SUITE_data/rest_nodelete_resource.erl | 13 +++++---- test/http_SUITE_data/rest_param_all.erl | 6 ++-- test/http_SUITE_data/rest_patch_resource.erl | 13 ++++++--- .../http_SUITE_data/rest_post_charset_resource.erl | 10 +++++-- test/http_SUITE_data/rest_postonly_resource.erl | 10 +++++-- test/http_SUITE_data/rest_resource_etags.erl | 10 +++++-- test/http_SUITE_data/rest_simple_resource.erl | 10 ++++--- test/ws_SUITE.erl | 33 +--------------------- test/ws_SUITE_data/ws_echo.erl | 17 ++++------- test/ws_SUITE_data/ws_echo_timer.erl | 17 ++++------- test/ws_SUITE_data/ws_init_shutdown.erl | 20 ++----------- test/ws_SUITE_data/ws_send_many.erl | 16 ++--------- test/ws_SUITE_data/ws_timeout_cancel.erl | 16 ++++------- test/ws_SUITE_data/ws_timeout_hibernate.erl | 16 ++++------- test/ws_SUITE_data/ws_upgrade_with_opts.erl | 28 ------------------ 37 files changed, 181 insertions(+), 299 deletions(-) delete mode 100644 test/ws_SUITE_data/ws_upgrade_with_opts.erl (limited to 'test') diff --git a/test/handlers/input_crash_h.erl b/test/handlers/input_crash_h.erl index 668d053..e941cca 100644 --- a/test/handlers/input_crash_h.erl +++ b/test/handlers/input_crash_h.erl @@ -3,8 +3,8 @@ -module(input_crash_h). --export([init/3]). +-export([init/2]). -init(_, Req, content_length) -> +init(Req, content_length) -> cowboy_error_h:ignore(cow_http_hd, number, 2), cowboy_req:parse_header(<<"content-length">>, Req). diff --git a/test/handlers/long_polling_h.erl b/test/handlers/long_polling_h.erl index 1c86aed..1b240fd 100644 --- a/test/handlers/long_polling_h.erl +++ b/test/handlers/long_polling_h.erl @@ -4,15 +4,14 @@ %% When it receives the last message, it sends a 102 reply back. -module(long_polling_h). --behaviour(cowboy_loop_handler). --export([init/3]). +-export([init/2]). -export([info/3]). -export([terminate/3]). -init(_, Req, _) -> +init(Req, _) -> erlang:send_after(200, self(), timeout), - {loop, Req, 2, 5000, hibernate}. + {long_polling, Req, 2, 5000, hibernate}. info(timeout, Req, 0) -> {ok, cowboy_req:reply(102, Req), 0}; diff --git a/test/handlers/loop_handler_body_h.erl b/test/handlers/loop_handler_body_h.erl index 559ef90..ac75773 100644 --- a/test/handlers/loop_handler_body_h.erl +++ b/test/handlers/loop_handler_body_h.erl @@ -4,15 +4,14 @@ %% then sends a 200 reply back. -module(loop_handler_body_h). --behaviour(cowboy_loop_handler). --export([init/3]). +-export([init/2]). -export([info/3]). -export([terminate/3]). -init(_, Req, _) -> +init(Req, _) -> self() ! timeout, - {loop, Req, undefined, 5000, hibernate}. + {long_polling, Req, undefined, 5000, hibernate}. info(timeout, Req, State) -> {ok, Body, Req2} = cowboy_req:body(Req), diff --git a/test/handlers/loop_handler_timeout_h.erl b/test/handlers/loop_handler_timeout_h.erl index 3b158bf..8e24d33 100644 --- a/test/handlers/loop_handler_timeout_h.erl +++ b/test/handlers/loop_handler_timeout_h.erl @@ -5,15 +5,14 @@ %% This results in a 204 reply being sent back by Cowboy. -module(loop_handler_timeout_h). --behaviour(cowboy_loop_handler). --export([init/3]). +-export([init/2]). -export([info/3]). -export([terminate/3]). -init(_, Req, _) -> +init(Req, _) -> erlang:send_after(1000, self(), timeout), - {loop, Req, undefined, 200, hibernate}. + {long_polling, Req, undefined, 200, hibernate}. info(timeout, Req, State) -> {ok, cowboy_req:reply(500, Req), State}. diff --git a/test/http_SUITE_data/http_body_qs.erl b/test/http_SUITE_data/http_body_qs.erl index 09eebdb..b219566 100644 --- a/test/http_SUITE_data/http_body_qs.erl +++ b/test/http_SUITE_data/http_body_qs.erl @@ -1,11 +1,12 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_body_qs). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). -init({_, http}, Req, _) -> - {ok, Req, undefined}. +-export([init/2]). +-export([handle/2]). + +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> Method = cowboy_req:method(Req), @@ -33,6 +34,3 @@ echo(Echo, Req) -> cowboy_req:reply(200, [ {<<"content-type">>, <<"text/plain; charset=utf-8">>} ], Echo, Req). - -terminate(_, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_chunked.erl b/test/http_SUITE_data/http_chunked.erl index 7f0d749..0c18363 100644 --- a/test/http_SUITE_data/http_chunked.erl +++ b/test/http_SUITE_data/http_chunked.erl @@ -1,11 +1,12 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_chunked). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). -init({_Transport, http}, Req, _Opts) -> - {ok, Req, undefined}. +-export([init/2]). +-export([handle/2]). + +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> Req2 = cowboy_req:chunked_reply(200, Req), @@ -14,6 +15,3 @@ handle(Req, State) -> timer:sleep(100), cowboy_req:chunk("works fine!", Req2), {ok, Req2, State}. - -terminate(_, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_echo_body.erl b/test/http_SUITE_data/http_echo_body.erl index 986015a..8743844 100644 --- a/test/http_SUITE_data/http_echo_body.erl +++ b/test/http_SUITE_data/http_echo_body.erl @@ -1,11 +1,12 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_echo_body). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). -init({_, http}, Req, _) -> - {ok, Req, undefined}. +-export([init/2]). +-export([handle/2]). + +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> true = cowboy_req:has_body(Req), @@ -22,6 +23,3 @@ handle_body(Req, Body) -> Size = cowboy_req:body_length(Req), Size = byte_size(Body), cowboy_req:reply(200, [], Body, Req). - -terminate(_, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_errors.erl b/test/http_SUITE_data/http_errors.erl index 7c3872b..39eda60 100644 --- a/test/http_SUITE_data/http_errors.erl +++ b/test/http_SUITE_data/http_errors.erl @@ -1,10 +1,11 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_errors). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). -init({_Transport, http}, Req, _Opts) -> +-export([init/2]). +-export([handle/2]). + +init(Req, _Opts) -> #{'case' := Case} = cowboy_req:match_qs(Req, ['case']), case_init(Case, Req). @@ -17,11 +18,11 @@ case_init(<<"init_after_reply">> = Case, Req) -> error(Case); case_init(<<"init_reply_handle_error">> = Case, Req) -> Req1 = cowboy_req:reply(200, [], "http_handler_crashes", Req), - {ok, Req1, Case}; + {http, Req1, Case}; case_init(<<"handle_before_reply">> = Case, Req) -> - {ok, Req, Case}; + {http, Req, Case}; case_init(<<"handle_after_reply">> = Case, Req) -> - {ok, Req, Case}. + {http, Req, Case}. handle(_Req, <<"init_reply_handle_error">> = Case) -> cowboy_error_h:ignore(?MODULE, handle, 2), @@ -33,6 +34,3 @@ handle(Req, <<"handle_after_reply">> = Case) -> cowboy_error_h:ignore(?MODULE, handle, 2), _ = cowboy_req:reply(200, [], "http_handler_crashes", Req), error(Case). - -terminate(_, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_handler.erl b/test/http_SUITE_data/http_handler.erl index 296c918..61c14f8 100644 --- a/test/http_SUITE_data/http_handler.erl +++ b/test/http_SUITE_data/http_handler.erl @@ -1,18 +1,14 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_handler). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). --record(state, {headers, body}). +-export([init/2]). +-export([handle/2]). -init({_Transport, http}, Req, Opts) -> - Headers = proplists:get_value(headers, Opts, []), - Body = proplists:get_value(body, Opts, "http_handler"), - {ok, Req, #state{headers=Headers, body=Body}}. +init(Req, Opts) -> + {http, Req, Opts}. -handle(Req, State=#state{headers=Headers, body=Body}) -> +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}. - -terminate(_, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_init_shutdown.erl b/test/http_SUITE_data/http_init_shutdown.erl index 512132e..5aae898 100644 --- a/test/http_SUITE_data/http_init_shutdown.erl +++ b/test/http_SUITE_data/http_init_shutdown.erl @@ -1,17 +1,10 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_init_shutdown). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). -init({_Transport, http}, Req, _Opts) -> +-export([init/2]). + +init(Req, _) -> Req2 = cowboy_req:reply(<<"666 Init Shutdown Testing">>, [{<<"connection">>, <<"close">>}], Req), {shutdown, Req2, undefined}. - -handle(Req, State) -> - Req2 = cowboy_req:reply(200, [], "Hello world!", Req), - {ok, Req2, State}. - -terminate(_, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_loop_stream_recv.erl b/test/http_SUITE_data/http_loop_stream_recv.erl index 77a339b..8547cc9 100644 --- a/test/http_SUITE_data/http_loop_stream_recv.erl +++ b/test/http_SUITE_data/http_loop_stream_recv.erl @@ -1,14 +1,15 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_loop_stream_recv). --export([init/3]). + +-export([init/2]). -export([info/3]). -export([terminate/3]). -init({_, http}, Req, _) -> +init(Req, _) -> receive after 100 -> ok end, self() ! stream, - {loop, Req, undefined, 100}. + {long_polling, Req, undefined, 100}. info(stream, Req, undefined) -> stream(Req, 1, <<>>). diff --git a/test/http_SUITE_data/http_multipart.erl b/test/http_SUITE_data/http_multipart.erl index 43ff6ab..6bd6408 100644 --- a/test/http_SUITE_data/http_multipart.erl +++ b/test/http_SUITE_data/http_multipart.erl @@ -1,19 +1,17 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_multipart). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). -init({_Transport, http}, Req, []) -> - {ok, Req, {}}. +-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}. -terminate(_, _, _) -> - ok. - acc_multipart(Req, Acc) -> case cowboy_req:part(Req) of {ok, Headers, Req2} -> diff --git a/test/http_SUITE_data/http_multipart_stream.erl b/test/http_SUITE_data/http_multipart_stream.erl index bde7531..43d459a 100644 --- a/test/http_SUITE_data/http_multipart_stream.erl +++ b/test/http_SUITE_data/http_multipart_stream.erl @@ -1,19 +1,17 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_multipart_stream). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). -init(_, Req, []) -> - {ok, Req, undefined}. +-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}. -terminate(_, _, _) -> - ok. - multipart(Req) -> case cowboy_req:part(Req) of {ok, [{<<"content-length">>, BinLength}], Req2} -> diff --git a/test/http_SUITE_data/http_req_attr.erl b/test/http_SUITE_data/http_req_attr.erl index 2a4a55d..9c5acba 100644 --- a/test/http_SUITE_data/http_req_attr.erl +++ b/test/http_SUITE_data/http_req_attr.erl @@ -1,18 +1,19 @@ %% Feel free to use, reuse and abuse the code in this file. +%% @todo That module was clearly meant to do more than one +%% thing and yet doesn't. -module(http_req_attr). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). -init({_, http}, Req, _) -> - #{attr := Attr} = cowboy_req:match_qs(Req, [attr]), - {ok, Req, Attr}. +-export([init/2]). +-export([handle/2]). + +init(Req, Opts) -> + {http, Req, Opts}. -handle(Req, <<"host_and_port">> = Attr) -> +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), Attr}. - -terminate(_, _, _) -> - ok. + {ok, cowboy_req:reply(200, [], Value, Req), State}. diff --git a/test/http_SUITE_data/http_set_resp.erl b/test/http_SUITE_data/http_set_resp.erl index 628f745..77196a8 100644 --- a/test/http_SUITE_data/http_set_resp.erl +++ b/test/http_SUITE_data/http_set_resp.erl @@ -1,10 +1,11 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_set_resp). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). -init({_Transport, http}, Req, Opts) -> +-export([init/2]). +-export([handle/2]). + +init(Req, Opts) -> Headers = proplists:get_value(headers, Opts, []), Body = proplists:get_value(body, Opts, <<"http_handler_set_resp">>), Req2 = lists:foldl(fun({Name, Value}, R) -> @@ -13,7 +14,7 @@ init({_Transport, http}, 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), - {ok, Req5, undefined}. + {http, Req5, undefined}. handle(Req, State) -> case cowboy_req:has_resp_header(<<"x-cowboy-test">>, Req) of @@ -26,6 +27,3 @@ handle(Req, State) -> {ok, cowboy_req:reply(200, Req), State} end end. - -terminate(_, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_stream_body.erl b/test/http_SUITE_data/http_stream_body.erl index 9be79a1..29569cd 100644 --- a/test/http_SUITE_data/http_stream_body.erl +++ b/test/http_SUITE_data/http_stream_body.erl @@ -1,18 +1,16 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_stream_body). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). --record(state, {headers, body, reply}). +-export([init/2]). +-export([handle/2]). -init({_Transport, http}, Req, Opts) -> - Headers = proplists:get_value(headers, Opts, []), - Body = proplists:get_value(body, Opts, "http_handler_stream_body"), - Reply = proplists:get_value(reply, Opts), - {ok, Req, #state{headers=Headers, body=Body, reply=Reply}}. +init(Req, Opts) -> + {http, Req, Opts}. -handle(Req, State=#state{headers=_Headers, body=Body, reply=Reply}) -> +handle(Req, State) -> + Body = proplists:get_value(body, State, "http_handler_stream_body"), + Reply = proplists:get_value(reply, State), SFun = fun(Socket, Transport) -> Transport:send(Socket, Body) end, Req2 = case Reply of set_resp -> @@ -26,6 +24,3 @@ handle(Req, State=#state{headers=_Headers, body=Body, reply=Reply}) -> cowboy_req:set_resp_body_fun(chunked, SFun2, Req) end, {ok, cowboy_req:reply(200, Req2), State}. - -terminate(_, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_streamed.erl b/test/http_SUITE_data/http_streamed.erl index 5f90077..ba710f1 100644 --- a/test/http_SUITE_data/http_streamed.erl +++ b/test/http_SUITE_data/http_streamed.erl @@ -1,11 +1,12 @@ %% Feel free to use, reuse and abuse the code in this file. -module(http_streamed). --behaviour(cowboy_http_handler). --export([init/3, handle/2, terminate/3]). -init({_Transport, http}, Req, _Opts) -> - {ok, Req, undefined}. +-export([init/2]). +-export([handle/2]). + +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> Req2 = cowboy_req:set([{resp_state, waiting_stream}], Req), @@ -15,6 +16,3 @@ handle(Req, State) -> timer:sleep(100), cowboy_req:chunk("works fine!", Req3), {ok, Req3, State}. - -terminate(_, _, _) -> - ok. diff --git a/test/http_SUITE_data/rest_empty_resource.erl b/test/http_SUITE_data/rest_empty_resource.erl index 7e7c00a..97fc26f 100644 --- a/test/http_SUITE_data/rest_empty_resource.erl +++ b/test/http_SUITE_data/rest_empty_resource.erl @@ -1,5 +1,6 @@ -module(rest_empty_resource). --export([init/3]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +-export([init/2]). + +init(Req, Opts) -> + {rest, Req, Opts}. diff --git a/test/http_SUITE_data/rest_expires.erl b/test/http_SUITE_data/rest_expires.erl index 4209041..e71b107 100644 --- a/test/http_SUITE_data/rest_expires.erl +++ b/test/http_SUITE_data/rest_expires.erl @@ -1,13 +1,13 @@ -module(rest_expires). --export([init/3]). +-export([init/2]). -export([content_types_provided/2]). -export([get_text_plain/2]). -export([expires/2]). -export([last_modified/2]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +init(Req, Opts) -> + {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 4cbd001..84b0675 100644 --- a/test/http_SUITE_data/rest_expires_binary.erl +++ b/test/http_SUITE_data/rest_expires_binary.erl @@ -1,12 +1,12 @@ -module(rest_expires_binary). --export([init/3]). +-export([init/2]). -export([content_types_provided/2]). -export([get_text_plain/2]). -export([expires/2]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +init(Req, Opts) -> + {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 d055193..1e6d99f 100644 --- a/test/http_SUITE_data/rest_forbidden_resource.erl +++ b/test/http_SUITE_data/rest_forbidden_resource.erl @@ -1,13 +1,15 @@ -module(rest_forbidden_resource). --export([init/3, rest_init/2, allowed_methods/2, forbidden/2, - content_types_provided/2, content_types_accepted/2, - to_text/2, from_text/2]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +-export([init/2]). +-export([allowed_methods/2]). +-export([forbidden/2]). +-export([content_types_provided/2]). +-export([content_types_accepted/2]). +-export([to_text/2]). +-export([from_text/2]). -rest_init(Req, [Forbidden]) -> - {ok, Req, Forbidden}. +init(Req, [Forbidden]) -> + {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 94bfbbd..fec308a 100644 --- a/test/http_SUITE_data/rest_missing_callbacks.erl +++ b/test/http_SUITE_data/rest_missing_callbacks.erl @@ -1,11 +1,12 @@ -module(rest_missing_callbacks). --export([init/3]). + +-export([init/2]). -export([allowed_methods/2]). -export([content_types_accepted/2]). -export([content_types_provided/2]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +init(Req, Opts) -> + {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 9f9670c..e8123db 100644 --- a/test/http_SUITE_data/rest_nodelete_resource.erl +++ b/test/http_SUITE_data/rest_nodelete_resource.erl @@ -1,17 +1,18 @@ -module(rest_nodelete_resource). --export([init/3, allowed_methods/2, content_types_provided/2, - get_text_plain/2]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +-export([init/2]). +-export([allowed_methods/2]). +-export([content_types_provided/2]). +-export([get_text_plain/2]). + +init(Req, Opts) -> + {rest, Req, Opts}. allowed_methods(Req, State) -> {[<<"GET">>, <<"HEAD">>, <<"DELETE">>], Req, State}. - content_types_provided(Req, State) -> {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. get_text_plain(Req, State) -> {<<"This is REST!">>, Req, State}. - diff --git a/test/http_SUITE_data/rest_param_all.erl b/test/http_SUITE_data/rest_param_all.erl index 22daac7..54950eb 100644 --- a/test/http_SUITE_data/rest_param_all.erl +++ b/test/http_SUITE_data/rest_param_all.erl @@ -1,14 +1,14 @@ -module(rest_param_all). --export([init/3]). +-export([init/2]). -export([allowed_methods/2]). -export([content_types_provided/2]). -export([get_text_plain/2]). -export([content_types_accepted/2]). -export([put_text_plain/2]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +init(Req, Opts) -> + {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 7b9b76e..4b81648 100644 --- a/test/http_SUITE_data/rest_patch_resource.erl +++ b/test/http_SUITE_data/rest_patch_resource.erl @@ -1,9 +1,14 @@ -module(rest_patch_resource). --export([init/3, allowed_methods/2, content_types_provided/2, get_text_plain/2, - content_types_accepted/2, patch_text_plain/2]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +-export([init/2]). +-export([allowed_methods/2]). +-export([content_types_provided/2]). +-export([get_text_plain/2]). +-export([content_types_accepted/2]). +-export([patch_text_plain/2]). + +init(Req, Opts) -> + {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 9ccfa61..7b7c49c 100644 --- a/test/http_SUITE_data/rest_post_charset_resource.erl +++ b/test/http_SUITE_data/rest_post_charset_resource.erl @@ -1,8 +1,12 @@ -module(rest_post_charset_resource). --export([init/3, allowed_methods/2, content_types_accepted/2, from_text/2]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +-export([init/2]). +-export([allowed_methods/2]). +-export([content_types_accepted/2]). +-export([from_text/2]). + +init(Req, Opts) -> + {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 4f725c9..9b14b24 100644 --- a/test/http_SUITE_data/rest_postonly_resource.erl +++ b/test/http_SUITE_data/rest_postonly_resource.erl @@ -1,8 +1,12 @@ -module(rest_postonly_resource). --export([init/3, allowed_methods/2, content_types_accepted/2, from_text/2]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +-export([init/2]). +-export([allowed_methods/2]). +-export([content_types_accepted/2]). +-export([from_text/2]). + +init(Req, Opts) -> + {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 fb266d1..9509d0a 100644 --- a/test/http_SUITE_data/rest_resource_etags.erl +++ b/test/http_SUITE_data/rest_resource_etags.erl @@ -1,8 +1,12 @@ -module(rest_resource_etags). --export([init/3, generate_etag/2, content_types_provided/2, get_text_plain/2]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +-export([init/2]). +-export([generate_etag/2]). +-export([content_types_provided/2]). +-export([get_text_plain/2]). + +init(Req, Opts) -> + {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 97145dd..3d2787f 100644 --- a/test/http_SUITE_data/rest_simple_resource.erl +++ b/test/http_SUITE_data/rest_simple_resource.erl @@ -1,12 +1,14 @@ -module(rest_simple_resource). --export([init/3, content_types_provided/2, get_text_plain/2]). -init(_Transport, _Req, _Opts) -> - {upgrade, protocol, cowboy_rest}. +-export([init/2]). +-export([content_types_provided/2]). +-export([get_text_plain/2]). + +init(Req, Opts) -> + {rest, Req, Opts}. content_types_provided(Req, State) -> {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. get_text_plain(Req, State) -> {<<"This is REST!">>, Req, State}. - diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl index 77c82f6..e341e10 100644 --- a/test/ws_SUITE.erl +++ b/test/ws_SUITE.erl @@ -80,9 +80,7 @@ init_dispatch() -> {text, <<"won't be received">>}]} ]}, {"/ws_timeout_hibernate", ws_timeout_hibernate, []}, - {"/ws_timeout_cancel", ws_timeout_cancel, []}, - {"/ws_upgrade_with_opts", ws_upgrade_with_opts, - <<"failure">>} + {"/ws_timeout_cancel", ws_timeout_cancel, []} ]} ]). @@ -653,35 +651,6 @@ ws_timeout_reset(Config) -> {error, closed} = gen_tcp:recv(Socket, 0, 6000), ok. -ws_upgrade_with_opts(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = gen_tcp:send(Socket, [ - "GET /ws_upgrade_with_opts HTTP/1.1\r\n" - "Host: localhost\r\n" - "Connection: Upgrade\r\n" - "Upgrade: websocket\r\n" - "Sec-WebSocket-Origin: http://localhost\r\n" - "Sec-WebSocket-Version: 8\r\n" - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" - "\r\n"]), - {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), - {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} - = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = do_decode_headers( - erlang:decode_packet(httph, Rest, []), []), - {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), - {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), - {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} - = lists:keyfind("sec-websocket-accept", 1, Headers), - {ok, Response} = gen_tcp:recv(Socket, 9, 6000), - << 1:1, 0:3, 1:4, 0:1, 7:7, "success" >> = Response, - ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), %% close - {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), - {error, closed} = gen_tcp:recv(Socket, 0, 6000), - ok. - %% Internal. do_decode_headers({ok, http_eoh, Rest}, Acc) -> diff --git a/test/ws_SUITE_data/ws_echo.erl b/test/ws_SUITE_data/ws_echo.erl index d4a5f07..c4ab406 100644 --- a/test/ws_SUITE_data/ws_echo.erl +++ b/test/ws_SUITE_data/ws_echo.erl @@ -1,17 +1,13 @@ %% Feel free to use, reuse and abuse the code in this file. -module(ws_echo). --behaviour(cowboy_websocket_handler). --export([init/3]). --export([websocket_init/3, websocket_handle/3, - websocket_info/3, websocket_terminate/3]). -init(_Any, _Req, _Opts) -> - {upgrade, protocol, cowboy_websocket}. +-export([init/2]). +-export([websocket_handle/3]). +-export([websocket_info/3]). -websocket_init(_TransportName, Req, _Opts) -> - Req2 = cowboy_req:compact(Req), - {ok, Req2, undefined}. +init(Req, _) -> + {ws, Req, undefined}. websocket_handle({text, Data}, Req, State) -> {reply, {text, Data}, Req, State}; @@ -22,6 +18,3 @@ websocket_handle(_Frame, Req, State) -> websocket_info(_Info, Req, State) -> {ok, Req, State}. - -websocket_terminate(_Reason, _Req, _State) -> - ok. diff --git a/test/ws_SUITE_data/ws_echo_timer.erl b/test/ws_SUITE_data/ws_echo_timer.erl index 666a26d..199f02c 100644 --- a/test/ws_SUITE_data/ws_echo_timer.erl +++ b/test/ws_SUITE_data/ws_echo_timer.erl @@ -1,18 +1,14 @@ %% Feel free to use, reuse and abuse the code in this file. -module(ws_echo_timer). --behaviour(cowboy_websocket_handler). --export([init/3]). --export([websocket_init/3, websocket_handle/3, - websocket_info/3, websocket_terminate/3]). -init(_Any, _Req, _Opts) -> - {upgrade, protocol, cowboy_websocket}. +-export([init/2]). +-export([websocket_handle/3]). +-export([websocket_info/3]). -websocket_init(_TransportName, Req, _Opts) -> +init(Req, _) -> erlang:start_timer(1000, self(), <<"websocket_init">>), - Req2 = cowboy_req:compact(Req), - {ok, Req2, undefined}. + {ws, Req, undefined}. websocket_handle({text, Data}, Req, State) -> {reply, {text, Data}, Req, State}; @@ -26,6 +22,3 @@ websocket_info({timeout, _Ref, Msg}, Req, State) -> {reply, {text, Msg}, Req, State}; websocket_info(_Info, Req, State) -> {ok, Req, State}. - -websocket_terminate(_Reason, _Req, _State) -> - ok. diff --git a/test/ws_SUITE_data/ws_init_shutdown.erl b/test/ws_SUITE_data/ws_init_shutdown.erl index 30bf66e..68f96f0 100644 --- a/test/ws_SUITE_data/ws_init_shutdown.erl +++ b/test/ws_SUITE_data/ws_init_shutdown.erl @@ -1,22 +1,8 @@ %% Feel free to use, reuse and abuse the code in this file. -module(ws_init_shutdown). --behaviour(cowboy_websocket_handler). --export([init/3]). --export([websocket_init/3, websocket_handle/3, - websocket_info/3, websocket_terminate/3]). -init(_Any, _Req, _Opts) -> - {upgrade, protocol, cowboy_websocket}. +-export([init/2]). -websocket_init(_TransportName, Req, _Opts) -> - {shutdown, cowboy_req:reply(403, Req)}. - -websocket_handle(_Frame, _Req, _State) -> - exit(badarg). - -websocket_info(_Info, _Req, _State) -> - exit(badarg). - -websocket_terminate(_Reason, _Req, _State) -> - exit(badarg). +init(Req, _) -> + {shutdown, cowboy_req:reply(403, Req), undefined}. diff --git a/test/ws_SUITE_data/ws_send_many.erl b/test/ws_SUITE_data/ws_send_many.erl index 2ed4772..2da82c3 100644 --- a/test/ws_SUITE_data/ws_send_many.erl +++ b/test/ws_SUITE_data/ws_send_many.erl @@ -1,27 +1,17 @@ %% Feel free to use, reuse and abuse the code in this file. -module(ws_send_many). --behaviour(cowboy_websocket_handler). --export([init/3]). --export([websocket_init/3]). +-export([init/2]). -export([websocket_handle/3]). -export([websocket_info/3]). --export([websocket_terminate/3]). -init(_Any, _Req, _Opts) -> - {upgrade, protocol, cowboy_websocket}. - -websocket_init(_TransportName, Req, Sequence) -> - Req2 = cowboy_req:compact(Req), +init(Req, Opts) -> erlang:send_after(10, self(), send_many), - {ok, Req2, Sequence}. + {ws, Req, Opts}. websocket_handle(_Frame, Req, State) -> {ok, Req, State}. websocket_info(send_many, Req, State = [{sequence, Sequence}]) -> {reply, Sequence, Req, State}. - -websocket_terminate(_Reason, _Req, _State) -> - ok. diff --git a/test/ws_SUITE_data/ws_timeout_cancel.erl b/test/ws_SUITE_data/ws_timeout_cancel.erl index 9c7b72b..6fcfc43 100644 --- a/test/ws_SUITE_data/ws_timeout_cancel.erl +++ b/test/ws_SUITE_data/ws_timeout_cancel.erl @@ -1,17 +1,14 @@ %% Feel free to use, reuse and abuse the code in this file. -module(ws_timeout_cancel). --behaviour(cowboy_websocket_handler). --export([init/3]). --export([websocket_init/3, websocket_handle/3, - websocket_info/3, websocket_terminate/3]). -init(_Any, _Req, _Opts) -> - {upgrade, protocol, cowboy_websocket}. +-export([init/2]). +-export([websocket_handle/3]). +-export([websocket_info/3]). -websocket_init(_TransportName, Req, _Opts) -> +init(Req, _) -> erlang:start_timer(500, self(), should_not_cancel_timer), - {ok, Req, undefined, 1000}. + {ws, Req, undefined, 1000}. websocket_handle({text, Data}, Req, State) -> {reply, {text, Data}, Req, State}; @@ -21,6 +18,3 @@ websocket_handle({binary, Data}, Req, State) -> websocket_info(_Info, Req, State) -> erlang:start_timer(500, self(), should_not_cancel_timer), {ok, Req, State}. - -websocket_terminate(_Reason, _Req, _State) -> - ok. diff --git a/test/ws_SUITE_data/ws_timeout_hibernate.erl b/test/ws_SUITE_data/ws_timeout_hibernate.erl index cc91e26..da901d7 100644 --- a/test/ws_SUITE_data/ws_timeout_hibernate.erl +++ b/test/ws_SUITE_data/ws_timeout_hibernate.erl @@ -1,22 +1,16 @@ %% Feel free to use, reuse and abuse the code in this file. -module(ws_timeout_hibernate). --behaviour(cowboy_websocket_handler). --export([init/3]). --export([websocket_init/3, websocket_handle/3, - websocket_info/3, websocket_terminate/3]). -init(_Any, _Req, _Opts) -> - {upgrade, protocol, cowboy_websocket}. +-export([init/2]). +-export([websocket_handle/3]). +-export([websocket_info/3]). -websocket_init(_TransportName, Req, _Opts) -> - {ok, Req, undefined, 1000, hibernate}. +init(Req, _) -> + {ws, Req, undefined, 1000, hibernate}. websocket_handle(_Frame, Req, State) -> {ok, Req, State, hibernate}. websocket_info(_Info, Req, State) -> {ok, Req, State, hibernate}. - -websocket_terminate(_Reason, _Req, _State) -> - ok. diff --git a/test/ws_SUITE_data/ws_upgrade_with_opts.erl b/test/ws_SUITE_data/ws_upgrade_with_opts.erl deleted file mode 100644 index b4f82fa..0000000 --- a/test/ws_SUITE_data/ws_upgrade_with_opts.erl +++ /dev/null @@ -1,28 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(ws_upgrade_with_opts). --behaviour(cowboy_websocket_handler). - --export([init/3]). --export([websocket_init/3]). --export([websocket_handle/3]). --export([websocket_info/3]). --export([websocket_terminate/3]). - -init(_Any, Req, _Opts) -> - {upgrade, protocol, cowboy_websocket, Req, <<"success">>}. - -websocket_init(_TransportName, Req, Response) -> - Req2 = cowboy_req:compact(Req), - erlang:send_after(10, self(), send_response), - {ok, Req2, Response}. - -websocket_handle(_Frame, Req, State) -> - {ok, Req, State}. - -websocket_info(send_response, Req, State = Response) - when is_binary(Response) -> - {reply, {text, Response}, Req, State}. - -websocket_terminate(_Reason, _Req, _State) -> - ok. -- cgit v1.2.3