@@ -302,7 +302,12 @@ init([Mod, {SockMod, Socket}, Opts]) ->
302302handle_cast (accept , #{socket := Socket ,
303303 socket_mod := SockMod ,
304304 socket_opts := Opts } = State ) ->
305- XMPPSocket = xmpp_socket :new (SockMod , Socket , Opts ),
305+ MaxStanzaSize = proplists :get_value (max_stanza_size , Opts , infinity ),
306+ MaxStanzaElements = proplists :get_value (max_stanza_elements , Opts , infinity ),
307+ PreAuthMaxStanzaSize = proplists :get_value (pre_auth_max_stanza_size , Opts , MaxStanzaSize ),
308+ PreAuthMaxStanzaElements = proplists :get_value (pre_auth_max_stanza_elements , Opts , MaxStanzaElements ),
309+ Opts2 = [{max_stanza_size , PreAuthMaxStanzaSize }, {max_stanza_elements , PreAuthMaxStanzaElements } | Opts ],
310+ XMPPSocket = xmpp_socket :new (SockMod , Socket , Opts2 ),
306311 SocketMonitor = xmpp_socket :monitor (XMPPSocket ),
307312 case xmpp_socket :peername (XMPPSocket ) of
308313 {ok , IP } ->
@@ -314,9 +319,14 @@ handle_cast(accept, #{socket := Socket,
314319 {stop , State4 } ->
315320 {stop , normal , State4 };
316321 State4 ->
317- case is_disconnected (State4 ) of
318- true -> noreply (State4 );
319- false -> handle_info ({tcp , Socket , <<>>}, State4 )
322+ State5 = if MaxStanzaSize /= PreAuthMaxStanzaSize orelse MaxStanzaElements /= PreAuthMaxStanzaElements ->
323+ State4 #{post_auth_stanza_limits => {MaxStanzaSize , MaxStanzaElements }};
324+ true ->
325+ State4
326+ end ,
327+ case is_disconnected (State5 ) of
328+ true -> noreply (State5 );
329+ false -> handle_info ({tcp , Socket , <<>>}, State5 )
320330 end
321331 end ;
322332 {error , _ } ->
@@ -862,10 +872,17 @@ process_stream_established(State) ->
862872 State1 = State #{stream_authenticated => true ,
863873 stream_state => established ,
864874 stream_timeout => infinity },
865- try callback (handle_stream_established , State1 )
866- catch _ :{? MODULE , undef } -> State1
875+ State2 = update_parser_limits (State1 ),
876+ try callback (handle_stream_established , State2 )
877+ catch _ :{? MODULE , undef } -> State2
867878 end .
868879
880+ update_parser_limits (#{socket := Socket , post_auth_stanza_limits := {MaxStanzaSize , MaxElementsCount }} = State ) ->
881+ Socket2 = xmpp_socket :change_limits (Socket , MaxStanzaSize , MaxElementsCount ),
882+ maps :remove (post_auth_stanza_limits , State #{socket => Socket2 });
883+ update_parser_limits (State ) ->
884+ State .
885+
869886-spec process_compress (compress (), state ()) -> state ().
870887process_compress (# compress {},
871888 #{stream_compressed := Compressed ,
@@ -1032,8 +1049,9 @@ process_sasl_success(Props, ServerOut,
10321049 case is_disconnected (State2 ) of
10331050 true -> State2 ;
10341051 false ->
1035- State3 = map_remove_keys (State2 , [sasl_state , sasl_mech , sasl_channel_bindings ]),
1036- State3 #{stream_id => xmpp_stream :new_id (),
1052+ State3 = update_parser_limits (State2 ),
1053+ State4 = map_remove_keys (State3 , [sasl_state , sasl_mech , sasl_channel_bindings ]),
1054+ State4 #{stream_id => xmpp_stream :new_id (),
10371055 stream_authenticated => true ,
10381056 stream_header_sent => false ,
10391057 stream_restarted => true ,
@@ -1161,13 +1179,14 @@ process_sasl2_success(Props, ServerOut,
11611179 case is_disconnected (State1 ) of
11621180 true -> State1 ;
11631181 false ->
1164- State2 = State1 #{stream_id => xmpp_stream :new_id (),
1165- stream_authenticated => true ,
1166- user => User },
1182+ State2 = update_parser_limits (
1183+ State1 #{stream_id => xmpp_stream :new_id (),
1184+ stream_authenticated => true ,
1185+ user => User }),
11671186 {State3 , NewEls , Results } =
1168- try callback (handle_sasl2_inline , InlineEls , State2 )
1169- catch _ :{? MODULE , undef } -> {State2 , InlineEls , []}
1170- end ,
1187+ try callback (handle_sasl2_inline , InlineEls , State2 )
1188+ catch _ :{? MODULE , undef } -> {State2 , InlineEls , []}
1189+ end ,
11711190
11721191 case NewEls of
11731192 {continue , Tasks } ->
0 commit comments