diff --git a/src/mod_bosh.erl b/src/mod_bosh.erl index 8bfb7c073c..bd0f5926cf 100644 --- a/src/mod_bosh.erl +++ b/src/mod_bosh.erl @@ -64,7 +64,7 @@ -type headers_list() :: [{binary(), binary()}]. %% Request State --record(rstate, {req_sid}). +-record(rstate, {req_sid, opts}). -type rstate() :: #rstate{}. -type req() :: cowboy_req:req(). @@ -141,12 +141,12 @@ node_cleanup(Acc, #{node := Node}, _) -> %%-------------------------------------------------------------------- -spec init(req(), mongoose_http_handler:options()) -> {cowboy_loop, req(), rstate()}. -init(Req, _Opts) -> +init(Req, Opts) -> ?LOG_DEBUG(#{what => bosh_init, req => Req}), Msg = init_msg(Req), self() ! Msg, %% Upgrade to cowboy_loop behaviour to enable long polling - {cowboy_loop, Req, #rstate{}}. + {cowboy_loop, Req, #rstate{opts = Opts}}. %% ok return keep the handler looping. @@ -275,11 +275,11 @@ check_event_type_streamstrart(Body) -> -spec forward_body(req(), exml:element(), rstate()) -> {ok, req(), rstate()} | {stop, req(), rstate()}. -forward_body(Req, #xmlel{} = Body, S) -> +forward_body(Req, #xmlel{} = Body, #rstate{opts = Opts} = S) -> Type = to_event_type(Body), case Type of streamstart -> - {SessionStarted, Req1} = maybe_start_session(Req, Body), + {SessionStarted, Req1} = maybe_start_session(Req, Body, Opts), case SessionStarted of true -> {ok, Req1, S}; @@ -319,15 +319,15 @@ get_session_socket(Sid) -> end. --spec maybe_start_session(req(), exml:element()) -> +-spec maybe_start_session(req(), exml:element(), map()) -> {SessionStarted :: boolean(), req()}. -maybe_start_session(Req, Body) -> +maybe_start_session(Req, Body, Opts) -> Domain = exml_query:attr(Body, <<"to">>), case mongoose_domain_api:get_domain_host_type(Domain) of {ok, HostType} -> case gen_mod:is_loaded(HostType, ?MODULE) of true -> - maybe_start_session_on_known_host(HostType, Req, Body); + maybe_start_session_on_known_host(HostType, Req, Body, Opts); false -> {false, terminal_condition(<<"host-unknown">>, Req)} end; @@ -335,11 +335,11 @@ maybe_start_session(Req, Body) -> {false, terminal_condition(<<"host-unknown">>, Req)} end. --spec maybe_start_session_on_known_host(mongooseim:host_type(), req(), exml:element()) -> +-spec maybe_start_session_on_known_host(mongooseim:host_type(), req(), exml:element(), map()) -> {SessionStarted :: boolean(), req()}. -maybe_start_session_on_known_host(HostType, Req, Body) -> +maybe_start_session_on_known_host(HostType, Req, Body, Opts) -> try - maybe_start_session_on_known_host_unsafe(HostType, Req, Body) + maybe_start_session_on_known_host_unsafe(HostType, Req, Body, Opts) catch error:Reason -> %% It's here because something catch-y was here before @@ -349,22 +349,22 @@ maybe_start_session_on_known_host(HostType, Req, Body) -> {false, Req1} end. --spec maybe_start_session_on_known_host_unsafe(mongooseim:host_type(), req(), exml:element()) -> +-spec maybe_start_session_on_known_host_unsafe(mongooseim:host_type(), req(), exml:element(), map()) -> {SessionStarted :: boolean(), req()}. -maybe_start_session_on_known_host_unsafe(HostType, Req, Body) -> +maybe_start_session_on_known_host_unsafe(HostType, Req, Body, Opts) -> %% Version isn't checked as it would be meaningless when supporting %% only a subset of the specification. {ok, NewBody} = set_max_hold(Body), Peer = cowboy_req:peer(Req), PeerCert = cowboy_req:cert(Req), - start_session(HostType, Peer, PeerCert, NewBody), + start_session(HostType, Peer, PeerCert, NewBody, Opts), {true, Req}. -spec start_session(mongooseim:host_type(), mongoose_transport:peer(), - binary() | undefined, exml:element()) -> any(). -start_session(HostType, Peer, PeerCert, Body) -> + binary() | undefined, exml:element(), map()) -> any(). +start_session(HostType, Peer, PeerCert, Body, Opts) -> Sid = make_sid(), - {ok, Socket} = mod_bosh_socket:start(HostType, Sid, Peer, PeerCert), + {ok, Socket} = mod_bosh_socket:start(HostType, Sid, Peer, PeerCert, Opts), store_session(Sid, Socket), handle_request(Socket, streamstart, Body), ?LOG_DEBUG(#{what => bosh_start_session, sid => Sid}). diff --git a/src/mod_bosh_socket.erl b/src/mod_bosh_socket.erl index c1937b24b8..1e9567f261 100644 --- a/src/mod_bosh_socket.erl +++ b/src/mod_bosh_socket.erl @@ -4,8 +4,8 @@ -behaviour(mongoose_c2s_socket). %% API --export([start/4, - start_link/4, +-export([start/5, + start_link/5, start_supervisor/0, is_supervisor_started/0, handle_request/2, @@ -49,7 +49,7 @@ closing/3, compress/1, compress/3, get_cached_responses/1, get_client_acks/1, get_handlers/1, get_peer_certificate/1, get_pending/1, get_pending/1, get_sockmod/1, normal/2, normal/3, - pause/2, send/2, set_client_acks/2, start_link/4]). + pause/2, send/2, set_client_acks/2, start_link/5]). -include("mongoose.hrl"). -include("jlib.hrl"). @@ -101,16 +101,16 @@ %%-------------------------------------------------------------------- -spec start(mongooseim:host_type(), mod_bosh:sid(), mongoose_transport:peer(), - binary() | undefined) -> + binary() | undefined, map()) -> {'error', _} | {'ok', 'undefined' | pid()} | {'ok', 'undefined' | pid(), _}. -start(HostType, Sid, Peer, PeerCert) -> - supervisor:start_child(?BOSH_SOCKET_SUP, [HostType, Sid, Peer, PeerCert]). +start(HostType, Sid, Peer, PeerCert, Opts) -> + supervisor:start_child(?BOSH_SOCKET_SUP, [HostType, Sid, Peer, PeerCert, Opts]). -spec start_link(mongooseim:host_type(), mod_bosh:sid(), mongoose_transport:peer(), - binary() | undefined) -> + binary() | undefined, map()) -> 'ignore' | {'error', _} | {'ok', pid()}. -start_link(HostType, Sid, Peer, PeerCert) -> - gen_fsm_compat:start_link(?MODULE, [{HostType, Sid, Peer, PeerCert}], []). +start_link(HostType, Sid, Peer, PeerCert, Opts) -> + gen_fsm_compat:start_link(?MODULE, [{HostType, Sid, Peer, PeerCert, Opts}], []). -spec start_supervisor() -> {ok, pid()} | {error, any()}. start_supervisor() -> @@ -192,18 +192,19 @@ get_cached_responses(Pid) -> %% {stop, StopReason} %% @end %%-------------------------------------------------------------------- --spec init([{mongooseim:host_type(), mod_bosh:sid(), mongoose_transport:peer(), undefined | binary()}]) -> +-spec init([{mongooseim:host_type(), mod_bosh:sid(), mongoose_transport:peer(), undefined | + binary(), map()}]) -> {ok, accumulate, state()}. -init([{HostType, Sid, Peer = {IPTuple, Port}, PeerCert}]) -> +init([{HostType, Sid, Peer, PeerCert, ListenerOpts}]) -> BoshSocket = #bosh_socket{sid = Sid, pid = self(), peer = Peer, peercert = PeerCert}, - C2SOpts = #{access => all, - shaper => none, - xml_socket => true, - max_stanza_size => 0, - hibernate_after => 0, - c2s_state_timeout => 5000, - backwards_compatible_session => true, - port => Port, ip_tuple => IPTuple, proto => tcp}, + C2SOpts = ListenerOpts#{access => all, + shaper => none, + xml_socket => true, + max_stanza_size => 0, + hibernate_after => 0, + c2s_state_timeout => 5000, + backwards_compatible_session => true, + proto => tcp}, {ok, C2SPid} = mongoose_c2s:start({?MODULE, BoshSocket, C2SOpts}, []), Opts = gen_mod:get_loaded_module_opts(HostType, mod_bosh), State = new_state(Sid, C2SPid, Opts),