From 0ff54b4365836fb2fd61df133106d8389e7c8b4d Mon Sep 17 00:00:00 2001 From: Sergey Elin Date: Thu, 27 Oct 2016 19:04:37 +0300 Subject: [PATCH 1/5] Fixed crash on undefined content-type in soap_cowboy_protocol --- src/soap_cowboy_protocol.erl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/soap_cowboy_protocol.erl b/src/soap_cowboy_protocol.erl index a2d632e..f02e986 100644 --- a/src/soap_cowboy_protocol.erl +++ b/src/soap_cowboy_protocol.erl @@ -84,7 +84,7 @@ handle_xml(Soap_req, Cowboy_state, Version_module) -> Content_type = soap_req:content_type(Soap_req3), %% get the soap message (Xml) from the request body {Xml, Soap_req4} = - case string:to_lower(lists:sublist(Content_type, 17)) of + case maybe_content_type(Content_type) of "multipart/related" -> %% soap with attachments, the message is in the first part try @@ -104,6 +104,11 @@ handle_xml(Soap_req, Cowboy_state, Version_module) -> Handler_resp = soap_server_handler:handle_message(Xml, Soap_req4), make_response(Handler_resp, Cowboy_state, Version_module). +maybe_content_type(undefined) -> + undefined; +maybe_content_type(Content_type) -> + string:to_lower(lists:sublist(Content_type, 17)). + mime_decode(Message, Content_type_header) -> Mime_parameters = lists:nthtail(17, Content_type_header), Parsed_parameters = soap_mime:parse_mime_parameters(Mime_parameters), From 79cfe947ec432bbbd53b8c890a0fcc4b919d38da Mon Sep 17 00:00:00 2001 From: define-null Date: Thu, 22 Dec 2016 09:54:05 +0100 Subject: [PATCH 2/5] Fix types based on dialyzer checks (#17) * Fix types based on dialyzer checks * Soap_body spec fix --- src/soap.hrl | 4 ++-- src/soap_client_util.erl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/soap.hrl b/src/soap.hrl index 45dfe08..deb9b55 100644 --- a/src/soap.hrl +++ b/src/soap.hrl @@ -25,7 +25,7 @@ name :: string(), operation :: atom(), %% name of function as used in handler module soap_action = [] :: string(), - wrapper_ns = [] :: string(), %% namespace for the wrapper element (in + wrapper_ns = [] :: string() | undefined, %% namespace for the wrapper element (in %% case of rpc style) op_type :: notification | request_response, in_type :: [{string(), atom()}] | atom(), %% the list type is only used @@ -47,7 +47,7 @@ target_ns :: string(), soap_ns :: string(), style :: string() | undefined, %% "rpc" | "document" - decoders :: [{string(), module}], + decoders :: [{string(), module}] | undefined, url :: string(), port :: string(), binding :: string(), diff --git a/src/soap_client_util.erl b/src/soap_client_util.erl index 4771762..44b45bb 100644 --- a/src/soap_client_util.erl +++ b/src/soap_client_util.erl @@ -55,7 +55,7 @@ fault_parser :: fun(), parser_state :: any(), soap_headers = [] :: [string() | tuple()], - soap_body :: string(), + soap_body :: string() | tuple(), is_fault = false :: boolean(), namespaces = [] :: [{prefix(), uri()}] }). From 8c6d918e3d23f942a3652a864a50313af0b25b12 Mon Sep 17 00:00:00 2001 From: Defnull Date: Wed, 11 Jan 2017 16:51:27 +0100 Subject: [PATCH 3/5] Provide extra callbacks for cowboy protocol 1,2 --- src/soap_cowboy_1_protocol.erl | 16 +++++++++++++--- src/soap_cowboy_2_protocol.erl | 14 +++++++++++--- src/soap_cowboy_protocol.erl | 31 ++++++++++++++----------------- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/soap_cowboy_1_protocol.erl b/src/soap_cowboy_1_protocol.erl index e108229..2e56299 100644 --- a/src/soap_cowboy_1_protocol.erl +++ b/src/soap_cowboy_1_protocol.erl @@ -27,7 +27,7 @@ -module(soap_cowboy_1_protocol). %% -behaviour(cowboy_sub_protocol). --export([upgrade/4]). +-export([upgrade/4, upgrade/5]). -export([enrich_req/2]). -export([respond/4]). @@ -48,9 +48,19 @@ {Implementation_handler::module(), Options::any()}) -> {ok, cowboy_req(), cowboy_env()}. upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}) -> - soap_cowboy_protocol:upgrade(Cowboy_req, Env, Soap_handler, - {Handler, Options}, cowboy_1, ?MODULE). + {ok, Message, Cowboy_req2} = cowboy_req:body(Cowboy_req), + upgrade(Cowboy_req2, Env, Soap_handler, {Handler, Options}, Message). +%% There might exist middleware that reads body from the cowboy_req, in which +%% case it will be no longer available while calling upgrade/4. In this case +%% you are responsible for propogating Body directly to upgrade/5 +-spec upgrade(Cowboy_req::cowboy_req(), Env::cowboy_env(), + Soap_handler::module(), + {Implementation_handler::module(), Options::any()}, Body::binary()) -> + {ok, cowboy_req(), cowboy_env()}. +upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}, Message) -> + soap_cowboy_protocol:upgrade(Cowboy_req, Env, Soap_handler, + {Handler, Options}, cowboy_1, ?MODULE, Message). enrich_req(Cowboy_req, Soap_req) -> {Method, Req2} = cowboy_req:method(Cowboy_req), diff --git a/src/soap_cowboy_2_protocol.erl b/src/soap_cowboy_2_protocol.erl index ef7237d..d4ad7d3 100644 --- a/src/soap_cowboy_2_protocol.erl +++ b/src/soap_cowboy_2_protocol.erl @@ -29,7 +29,7 @@ -module(soap_cowboy_2_protocol). %%-behaviour(cowboy_sub_protocol). --export([upgrade/6]). +-export([upgrade/6, upgrade/7]). -export([enrich_req/2]). -export([respond/4]). @@ -48,9 +48,17 @@ -spec upgrade(Cowboy_req::cowboy_req(), Env::cowboy_env(), Soap_handler::module(), {Implementation_handler::module(), Options::any()}, Timeout::any(), Hibernate::any()) -> {ok, cowboy_req(), cowboy_env()}. -upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}, _, _) -> +upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}, Timeout, Hibernate) -> + {ok, Message, Cowboy_req2} = cowboy_req:body(Cowboy_req), + upgrade(Cowboy_req2, Env, Soap_handler, {Handler, Options}, Timeout, Hibernate, Message). + +%% There might exist middleware that reads body from the cowboy_req, in which +%% case it will be no longer available while calling upgrade/6. In this case +%% you are responsible for propogating Body directly to upgrade/7 +upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}, _, _, Message) -> soap_cowboy_protocol:upgrade(Cowboy_req, Env, Soap_handler, - {Handler, Options}, cowboy_2, ?MODULE). + {Handler, Options}, cowboy_2, ?MODULE, Message). + enrich_req(Cowboy_req, Soap_req) -> Method = cowboy_req:method(Cowboy_req), diff --git a/src/soap_cowboy_protocol.erl b/src/soap_cowboy_protocol.erl index f02e986..e1fc672 100644 --- a/src/soap_cowboy_protocol.erl +++ b/src/soap_cowboy_protocol.erl @@ -28,7 +28,7 @@ -module(soap_cowboy_protocol). --export([upgrade/6]). +-export([upgrade/7]). -record(state, { env :: cowboy_middleware:env(), @@ -51,12 +51,12 @@ -spec upgrade(Cowboy_req::cowboy_req(), Env::cowboy_env(), Soap_handler::module(), {Implementation_handler::module(), Options::any()}, Version::atom(), - Version_module::module()) -> {ok, cowboy_req(), cowboy_env()}. -upgrade(Cowboy_req, Env, _, {Handler, Options}, Version, Version_module) -> + Version_module::module(), Body::binary()) -> {ok, cowboy_req(), cowboy_env()}. +upgrade(Cowboy_req, Env, _, {Handler, Options}, Version, Version_module, Body) -> Cowboy_state = #state{env = Env, handler = Handler}, case soap_server_handler:new_req(Handler, Version, Options, Cowboy_req) of {continue, Soap_req} -> - check_conformance(Soap_req, Cowboy_req, Cowboy_state, Version_module); + check_conformance(Soap_req, Cowboy_req, Cowboy_state, Version_module, Body); {ok, _StatusCode, _Headers, _Body, _Server_req} = Error -> make_response(Error, Cowboy_state, Version_module) end. @@ -65,25 +65,22 @@ upgrade(Cowboy_req, Env, _, {Handler, Options}, Version, Version_module) -> %%% Internal functions %%% ============================================================================ -check_conformance(Soap_req, Cowboy_req, Cowboy_state, Version_module) -> +check_conformance(Soap_req, Cowboy_req, Cowboy_state, Version_module, Body) -> %% collect some information about the protocol, so that %% conformance can be checked. Soap_req2 = Version_module:enrich_req(Cowboy_req, Soap_req), case soap_server_handler:check_http_conformance(Soap_req2) of {continue, Soap_req3} -> - handle_xml(Soap_req3, Cowboy_state, Version_module); + handle_xml(Soap_req3, Cowboy_state, Version_module, Body); {ok, _StatusCode, _Headers, _Body, _Server_req} = Error -> make_response(Error, Cowboy_state, Version_module) end. -handle_xml(Soap_req, Cowboy_state, Version_module) -> - Cowboy_req = soap_req:server_req(Soap_req), - {ok, Message, Cowboy_req2} = cowboy_req:body(Cowboy_req), - Soap_req2 = soap_req:set_server_req(Soap_req, Cowboy_req2), - Soap_req3 = soap_req:set_http_body(Soap_req2, Message), - Content_type = soap_req:content_type(Soap_req3), +handle_xml(Soap_req, Cowboy_state, Version_module, Message) -> + Soap_req2 = soap_req:set_http_body(Soap_req, Message), + Content_type = soap_req:content_type(Soap_req2), %% get the soap message (Xml) from the request body - {Xml, Soap_req4} = + {Xml, Soap_req3} = case maybe_content_type(Content_type) of "multipart/related" -> %% soap with attachments, the message is in the first part @@ -92,16 +89,16 @@ handle_xml(Soap_req, Cowboy_state, Version_module) -> mime_decode(Message, Content_type), {Body, soap_req:set_mime_headers( - soap_req:set_req_attachments(Soap_req3, Attachments), + soap_req:set_req_attachments(Soap_req2, Attachments), Mime_headers)} catch _Class:_Type -> - {Message, Soap_req3} + {Message, Soap_req2} end; _ -> - {Message, Soap_req3} + {Message, Soap_req2} end, - Handler_resp = soap_server_handler:handle_message(Xml, Soap_req4), + Handler_resp = soap_server_handler:handle_message(Xml, Soap_req3), make_response(Handler_resp, Cowboy_state, Version_module). maybe_content_type(undefined) -> From 3a65a04e08d58f9a673363e48c81a0e6646dff12 Mon Sep 17 00:00:00 2001 From: Defnull Date: Wed, 18 Jan 2017 15:40:08 +0100 Subject: [PATCH 4/5] Add test for cowboy middle protocol support --- Makefile | 2 +- test/soap_SUITE.erl | 19 +++++++++ test/test_cowboy_middle_protocol.erl | 61 ++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 test/test_cowboy_middle_protocol.erl diff --git a/Makefile b/Makefile index cdda91f..b785432 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ ct: compile test_deps -@ct_run -noshell -pa ebin \ -pa deps/*/ebin \ -pa test/deps/*/ebin \ - -name test \ + -sname test \ -logdir ./logs \ -env TEST_DIR ./test \ -spec ./test/test_specs.spec \ diff --git a/test/soap_SUITE.erl b/test/soap_SUITE.erl index ef21ffb..1b027c3 100644 --- a/test/soap_SUITE.erl +++ b/test/soap_SUITE.erl @@ -112,6 +112,12 @@ groups() -> ,{soap_11_client, [sequence], [client_11_ok ,client_11_fault]} ,{soap_12_client, [sequence], [client_12_ok ,client_12_fault]} ]} + ,{cowboy_middle_protocol, [sequence], + [correct_sms + ,sms_fault + ,sms_invalid_xml + ,sms_wrong_method + ]} ,{mochi_server, [sequence], [correct_sms ,sms_fault @@ -200,6 +206,15 @@ init_per_group(cowboy_server, Config) -> {"[0-9]{13,}", throw}, {http_server, cowboy_version()}]), Config; +init_per_group(cowboy_middle_protocol, Config) -> + {ok, _} = soap:start_server(sendService_test_server, + [{"[a-zA-Z]+", invalid}, + {"^\\+?[0-9]{4,12}$", valid}, + {"[0-9]{13,}", throw}, + {http_server, test_cowboy_middle_protocol}, + {cowboy_version, cowboy_version()} + ]), + Config; init_per_group(inets_server, Config) -> {ok, _} = soap:start_server(sendService_test_server, [{http_server, soap_server_inets}, @@ -315,6 +330,9 @@ end_per_group(inets_server, _Config) -> end_per_group(cowboy_server, _Config) -> soap:stop_server(sendService_test_server), ok; +end_per_group(cowboy_middle_protocol, _Config) -> + soap:stop_server(sendService_test_server), + ok; end_per_group(tempconvert_local, _Config) -> soap:stop_server(tempconvert_server), ok; @@ -345,6 +363,7 @@ end_per_group(_, _Config) -> %%-------------------------------------------------------------------- all() -> [{group, cowboy_server}, + {group, cowboy_middle_protocol}, {group, inets_server}, {group, tempconvert}, {group, tempconvert_12}, diff --git a/test/test_cowboy_middle_protocol.erl b/test/test_cowboy_middle_protocol.erl new file mode 100644 index 0000000..8cf37eb --- /dev/null +++ b/test/test_cowboy_middle_protocol.erl @@ -0,0 +1,61 @@ +-module(test_cowboy_middle_protocol). + +-export([start/1]). +-export([start/2]). +-export([stop/0]). +-export([init/3, init/2]). +-export([on_request/1]). +-export([upgrade/4, upgrade/6]). + +start(Module) -> + start(Module, []). + +start(Module, Options) -> + + Port = proplists:get_value(port, Options, 8080), + Acceptors = proplists:get_value(nr_acceptors, Options, 100), + {ok, _} = application:ensure_all_started(cowboy), + Dispatch = cowboy_router:compile([ + {'_', [{'_', ?MODULE, {Module, Options}}]}]), + {ok, _} = cowboy:start_http(http, Acceptors, [{port, Port}], + [ {env, [{dispatch, Dispatch}]}, + {onrequest, fun ?MODULE:on_request/1} + ]). + +stop() -> + cowboy:stop_listener(http), + application:stop(cowboy), + application:stop(ranch). + +on_request(Req0) -> + {ok, Body, Req1} = cowboy_req:body(Req0), + cowboy_req:set_meta(body, Body, Req1). + +%% cowboy 1 callback +init(_, _Req, {_Module, Options}) -> + %% The module 'soap_cowboy_protocol' will be called + %% for each request, with Module (= the handler module) and + %% the options as parameter. + soap_server_cowboy_1 = proplists:get_value(cowboy_version, Options), + {upgrade, protocol, ?MODULE}. + +%% cowboy 1 callback +upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}) -> + Options2 = proplists:delete(cowboy_version, Options), + {Message, Cowboy_req2} = cowboy_req:meta(body, Cowboy_req), + + soap_cowboy_1_protocol:upgrade( + Cowboy_req2, Env, Soap_handler, {Handler, Options2}, Message). + +%% cowboy 2 callback +init(Req, {Module, Options}) -> + soap_server_cowboy_2 = proplists:get_value(cowboy_version, Options), + {?MODULE, Req, {Module, Options}}. + +%% cowboy 2 callback +upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}, Timeout, Hibernate) -> + Options2 = proplists:delete(cowboy_version, Options), + {Message, Cowboy_req2} = cowboy_req:meta(body, Cowboy_req), + + soap_cowboy_2_protocol:upgrade( + Cowboy_req2, Env, Soap_handler, {Handler, Options2}, Timeout, Hibernate, Message). From ef20303cdf533acaec369c7497d91dd996ad7962 Mon Sep 17 00:00:00 2001 From: Defnull Date: Mon, 26 Mar 2018 11:28:13 +0200 Subject: [PATCH 5/5] Fix cowboy 2 support Set ibrowse version to point to the latest stable, add logs dir. --- Makefile | 1 + src/soap_cowboy_2_protocol.erl | 23 +++++----- src/soap_server_cowboy_1.erl | 19 ++++---- src/soap_server_cowboy_2.erl | 18 ++++---- test/rebar.config | 7 +-- test/soap_SUITE.erl | 2 +- test/test_cowboy_middle_protocol.erl | 67 ++++++++++++++++------------ 7 files changed, 72 insertions(+), 65 deletions(-) diff --git a/Makefile b/Makefile index b785432..aa1bc75 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ compile: @rebar compile ct: compile test_deps + mkdir -p ./logs @echo "Running common tests..." -@ct_run -noshell -pa ebin \ -pa deps/*/ebin \ diff --git a/src/soap_cowboy_2_protocol.erl b/src/soap_cowboy_2_protocol.erl index d4ad7d3..09453d7 100644 --- a/src/soap_cowboy_2_protocol.erl +++ b/src/soap_cowboy_2_protocol.erl @@ -29,7 +29,7 @@ -module(soap_cowboy_2_protocol). %%-behaviour(cowboy_sub_protocol). --export([upgrade/6, upgrade/7]). +-export([upgrade/4, upgrade/5]). -export([enrich_req/2]). -export([respond/4]). @@ -46,20 +46,19 @@ %% This callback is expected to behave like a middleware and to return an %% updated req object and environment. -spec upgrade(Cowboy_req::cowboy_req(), Env::cowboy_env(), - Soap_handler::module(), {Implementation_handler::module(), Options::any()}, - Timeout::any(), Hibernate::any()) -> {ok, cowboy_req(), cowboy_env()}. -upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}, Timeout, Hibernate) -> - {ok, Message, Cowboy_req2} = cowboy_req:body(Cowboy_req), - upgrade(Cowboy_req2, Env, Soap_handler, {Handler, Options}, Timeout, Hibernate, Message). + Soap_handler::module(), {Implementation_handler::module(), Options::any()}) + -> {ok, cowboy_req(), cowboy_env()}. +upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}) -> + {ok, Message, Cowboy_req2} = cowboy_req:read_body(Cowboy_req), + upgrade(Cowboy_req2, Env, Soap_handler, {Handler, Options}, Message). -%% There might exist middleware that reads body from the cowboy_req, in which -%% case it will be no longer available while calling upgrade/6. In this case -%% you are responsible for propogating Body directly to upgrade/7 -upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}, _, _, Message) -> - soap_cowboy_protocol:upgrade(Cowboy_req, Env, Soap_handler, +%% There might exist middleware that reads body from the cowboy_req, in which +%% case it will be no longer available while calling upgrade/4. In this case +%% you are responsible for propogating Body directly to upgrade/5 +upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}, Message) -> + soap_cowboy_protocol:upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}, cowboy_2, ?MODULE, Message). - enrich_req(Cowboy_req, Soap_req) -> Method = cowboy_req:method(Cowboy_req), Soap_req2 = soap_req:set_method(Soap_req, make_list(Method)), diff --git a/src/soap_server_cowboy_1.erl b/src/soap_server_cowboy_1.erl index 1fcb003..b1a286d 100644 --- a/src/soap_server_cowboy_1.erl +++ b/src/soap_server_cowboy_1.erl @@ -63,17 +63,14 @@ start(Module) -> start(Module, []). start(Module, Options) -> - Port = proplists:get_value(port, Options, 8080), - Acceptors = proplists:get_value(nr_acceptors, Options, 100), - ok = application:ensure_started(crypto), - ok = application:ensure_started(ranch), - ok = application:ensure_started(cowlib), - ok = application:ensure_started(cowboy), - Dispatch = cowboy_router:compile([ - {'_', [{'_', ?MODULE, {Module, Options}}]}]), - {ok, _} = cowboy:start_http(http, Acceptors, [{port, Port}], [ - {env, [{dispatch, Dispatch}]}]). - + Port = proplists:get_value(port, Options, 8080), + Acceptors = proplists:get_value(nr_acceptors, Options, 100), + {ok, _} = application:ensure_all_started(cowboy), + Dispatch = cowboy_router:compile([ + {'_', [{'_', ?MODULE, {Module, Options}}]}]), + {ok, _} = cowboy:start_http(http, Acceptors, [{port, Port}], + [{env, [{dispatch, Dispatch}]}]). + stop() -> cowboy:stop_listener(http), application:stop(cowboy), diff --git a/src/soap_server_cowboy_2.erl b/src/soap_server_cowboy_2.erl index 2f8bd95..0efc148 100644 --- a/src/soap_server_cowboy_2.erl +++ b/src/soap_server_cowboy_2.erl @@ -65,15 +65,15 @@ start(Module) -> start(Module, Options) -> Port = proplists:get_value(port, Options, 8080), Acceptors = proplists:get_value(nr_acceptors, Options, 100), - ok = application:ensure_started(crypto), - ok = application:ensure_started(ranch), - ok = application:ensure_started(cowlib), - ok = application:ensure_started(cowboy), - Dispatch = cowboy_router:compile([ - {'_', [{'_', ?MODULE, {Module, Options}}]}]), - {ok, _} = cowboy:start_http(http, Acceptors, [{port, Port}], [ - {env, [{dispatch, Dispatch}]}]). - + {ok, _} = application:ensure_all_started(cowboy), + Dispatch = cowboy_router:compile( + [ + {'_', [{'_', ?MODULE, {Module, Options}}]}]), + + {ok, _} = cowboy:start_clear(http, [{port, Port}], + #{env => #{dispatch => Dispatch}} + ). + stop() -> cowboy:stop_listener(http), application:stop(cowboy), diff --git a/test/rebar.config b/test/rebar.config index 2971d4b..55256e4 100644 --- a/test/rebar.config +++ b/test/rebar.config @@ -1,8 +1,9 @@ {erl_opts, [debug_info]}. {deps, [ - {ibrowse, ".*", {git, "https://github.com/cmullaparthi/ibrowse.git" }}, - {cowboy, ".*", {git, "https://github.com/ninenines/cowboy.git", {branch, "1.1.x"}}}, - {mochiweb, ".*", {git, "https://github.com/mochi/mochiweb.git" }}, + {ibrowse, ".*", {git, "https://github.com/cmullaparthi/ibrowse.git", {tag, "v4.4.0"} }}, +%% {cowboy, ".*", {git, "https://github.com/ninenines/cowboy.git", {tag, "1.1.2"}}}, + {cowboy, ".*", {git, "https://github.com/ninenines/cowboy.git", {tag, "2.2.2"}}}, + {mochiweb, ".*", {git, "https://github.com/mochi/mochiweb.git" }}, {erlsom, ".*", {git, "https://github.com/willemdj/erlsom.git", {branch, "v1_4"}}} ]}. diff --git a/test/soap_SUITE.erl b/test/soap_SUITE.erl index 1b027c3..ff878bf 100644 --- a/test/soap_SUITE.erl +++ b/test/soap_SUITE.erl @@ -930,7 +930,7 @@ cowboy_version() -> Info = cowboy:module_info(), Exports = proplists:get_value(exports, Info), case proplists:get_value(start_tls, Exports) of - 4 -> + 3 -> soap_server_cowboy_2; undefined -> soap_server_cowboy_1 diff --git a/test/test_cowboy_middle_protocol.erl b/test/test_cowboy_middle_protocol.erl index 8cf37eb..dba812e 100644 --- a/test/test_cowboy_middle_protocol.erl +++ b/test/test_cowboy_middle_protocol.erl @@ -1,61 +1,70 @@ -module(test_cowboy_middle_protocol). --export([start/1]). -export([start/2]). -export([stop/0]). -export([init/3, init/2]). -export([on_request/1]). --export([upgrade/4, upgrade/6]). +-export([execute/2]). +-export([upgrade/4]). -start(Module) -> - start(Module, []). start(Module, Options) -> - Port = proplists:get_value(port, Options, 8080), - Acceptors = proplists:get_value(nr_acceptors, Options, 100), {ok, _} = application:ensure_all_started(cowboy), Dispatch = cowboy_router:compile([ {'_', [{'_', ?MODULE, {Module, Options}}]}]), - {ok, _} = cowboy:start_http(http, Acceptors, [{port, Port}], - [ {env, [{dispatch, Dispatch}]}, - {onrequest, fun ?MODULE:on_request/1} - ]). + + case proplists:get_value(cowboy_version, Options) of + soap_server_cowboy_1 -> + cowboy:start_http(http, 10, [{port, Port}], + [ {env, [{dispatch, Dispatch}]}, + {onrequest, fun ?MODULE:on_request/1} + ]); + soap_server_cowboy_2 -> + cowboy:start_clear(http, [{port, Port}], + #{env => #{dispatch => Dispatch}, + middlewares => [ cowboy_router + , ?MODULE + , cowboy_handler + ] + } + ) + end. stop() -> cowboy:stop_listener(http), - application:stop(cowboy), + application:stop(cowboy), application:stop(ranch). on_request(Req0) -> {ok, Body, Req1} = cowboy_req:body(Req0), cowboy_req:set_meta(body, Body, Req1). - + +execute(Req, Env) -> + {ok, Body, Req1} = cowboy_req:read_body(Req), + {ok, Req1, maps:put(req_body, Body, Env)}. + %% cowboy 1 callback init(_, _Req, {_Module, Options}) -> %% The module 'soap_cowboy_protocol' will be called - %% for each request, with Module (= the handler module) and - %% the options as parameter. + %% for each request, with Module (= the handler module) and + %% the options as parameter. soap_server_cowboy_1 = proplists:get_value(cowboy_version, Options), {upgrade, protocol, ?MODULE}. -%% cowboy 1 callback -upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}) -> - Options2 = proplists:delete(cowboy_version, Options), - {Message, Cowboy_req2} = cowboy_req:meta(body, Cowboy_req), - - soap_cowboy_1_protocol:upgrade( - Cowboy_req2, Env, Soap_handler, {Handler, Options2}, Message). - %% cowboy 2 callback init(Req, {Module, Options}) -> soap_server_cowboy_2 = proplists:get_value(cowboy_version, Options), {?MODULE, Req, {Module, Options}}. -%% cowboy 2 callback -upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}, Timeout, Hibernate) -> - Options2 = proplists:delete(cowboy_version, Options), - {Message, Cowboy_req2} = cowboy_req:meta(body, Cowboy_req), - - soap_cowboy_2_protocol:upgrade( - Cowboy_req2, Env, Soap_handler, {Handler, Options2}, Timeout, Hibernate, Message). +upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}) -> + case proplists:get_value(cowboy_version, Options) of + soap_server_cowboy_1 -> + Options2 = proplists:delete(cowboy_version, Options), + {Message, Cowboy_req2} = cowboy_req:meta(body, Cowboy_req), + soap_cowboy_1_protocol:upgrade(Cowboy_req2, Env, Soap_handler, {Handler, Options2}, Message); + soap_server_cowboy_2 -> + Options2 = proplists:delete(cowboy_version, Options), + Message = maps:get(req_body, Env), + soap_cowboy_2_protocol:upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options2}, Message) + end.