Skip to content

Commit

Permalink
Fix cowboy 2 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
define-null committed Mar 26, 2018
1 parent 8c1a7c1 commit 8bb49b3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
21 changes: 10 additions & 11 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 @@ -47,19 +47,18 @@
%% 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).
Timeout::any(), Hibernate::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
29 changes: 14 additions & 15 deletions test/test_cowboy_middle_protocol.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,50 @@ 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}],
{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(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.
%% 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 2 callback
init(Req, {Module, Options}) ->
soap_server_cowboy_2 = proplists:get_value(cowboy_version, Options),
{?MODULE, Req, {Module, Options}}.

%% cowboy 1 callback
upgrade(Cowboy_req, Env, Soap_handler, {Handler, Options}) ->
upgrade(Cowboy_req, Env, Soap_handler, {soap_server_cowboy_1, 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_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) ->
upgrade(Cowboy_req, Env, Soap_handler, {soap_server_cowboy_2, Options}) ->
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).
Cowboy_req2, Env, Soap_handler, {Handler, Options2}, Message).

0 comments on commit 8bb49b3

Please sign in to comment.