From bac2faea84357980ce20d5ebcaa34f0550e0447a Mon Sep 17 00:00:00 2001 From: Slava Yurin Date: Wed, 26 Feb 2014 18:38:10 +0700 Subject: Fix inherit listen options for accepted socket Order of options in listen is undocumented but significant. Now user option will replace default value if user set it. --- src/ranch.erl | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/ranch.erl b/src/ranch.erl index 17d24d2..c7e9727 100644 --- a/src/ranch.erl +++ b/src/ranch.erl @@ -120,18 +120,27 @@ set_protocol_options(Ref, Opts) -> -spec filter_options([{atom(), any()} | {raw, any(), any(), any()}], [atom()], Acc) -> Acc when Acc :: [any()]. -filter_options([], _, Acc) -> - Acc; -filter_options([Opt = {Key, _}|Tail], AllowedKeys, Acc) -> +filter_options(UserOptions, AllowedKeys, DefaultOptions) -> + AllowedOptions = filter_user_options(UserOptions, AllowedKeys), + lists:foldl(fun merge_options/2, DefaultOptions, AllowedOptions). + +filter_user_options([Opt = {Key, _}|Tail], AllowedKeys) -> case lists:member(Key, AllowedKeys) of - true -> filter_options(Tail, AllowedKeys, [Opt|Acc]); - false -> filter_options(Tail, AllowedKeys, Acc) + true -> [Opt|filter_user_options(Tail, AllowedKeys)]; + false -> filter_user_options(Tail, AllowedKeys) end; -filter_options([Opt = {raw, _, _, _}|Tail], AllowedKeys, Acc) -> +filter_user_options([Opt = {raw, _, _, _}|Tail], AllowedKeys) -> case lists:member(raw, AllowedKeys) of - true -> filter_options(Tail, AllowedKeys, [Opt|Acc]); - false -> filter_options(Tail, AllowedKeys, Acc) - end. + true -> [Opt|filter_user_options(Tail, AllowedKeys)]; + false -> filter_user_options(Tail, AllowedKeys) + end; +filter_user_options([], _) -> + []. + +merge_options({Key, _} = Option, OptionList) -> + lists:keystore(Key, 1, OptionList, Option); +merge_options(Option, OptionList) -> + [Option|OptionList]. -spec set_option_default(Opts, atom(), any()) -> Opts when Opts :: [{atom(), any()}]. -- cgit v1.2.3