Skip to content

Commit

Permalink
Fix cowboy 2 support
Browse files Browse the repository at this point in the history
Set ibrowse version to point to the latest stable, add logs dir.
  • Loading branch information
define-null committed Mar 28, 2018
1 parent 8c1a7c1 commit 8cc6d6b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 65 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
23 changes: 11 additions & 12 deletions src/soap_cowboy_2_protocol.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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]).

Expand All @@ -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)),
Expand Down
19 changes: 8 additions & 11 deletions src/soap_server_cowboy_1.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
18 changes: 9 additions & 9 deletions src/soap_server_cowboy_2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 4 additions & 3 deletions test/rebar.config
Original file line number Diff line number Diff line change
@@ -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"}}}
]}.
2 changes: 1 addition & 1 deletion test/soap_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,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
Expand Down
67 changes: 38 additions & 29 deletions test/test_cowboy_middle_protocol.erl
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 8cc6d6b

Please sign in to comment.