11/* *
2- @file BNCURLBlackList .m
2+ @file BNCURLFilter .m
33 @package Branch-SDK
44 @brief Manages a list of URLs that we should ignore.
55
88 @copyright Copyright © 2018 Branch. All rights reserved.
99*/
1010
11- #import " BNCURLBlackList .h"
11+ #import " BNCURLFilter .h"
1212#import " Branch.h"
1313
14- @interface BNCURLBlackList () {
15- NSArray <NSString *>*_blackList ;
14+ @interface BNCURLFilter () {
15+ NSArray <NSString *>*_patternList ;
1616}
17- @property (strong ) NSArray <NSRegularExpression*> *blackListRegex ;
18- @property (assign ) NSInteger blackListVersion ;
17+ @property (strong ) NSArray <NSRegularExpression*> *ignoredURLRegex ;
18+ @property (assign ) NSInteger listVersion ;
1919@property (strong ) id <BNCNetworkServiceProtocol> networkService;
20- @property (assign ) BOOL hasRefreshedBlackListFromServer ;
20+ @property (assign ) BOOL hasUpdatedPatternList ;
2121@property (strong ) NSError *error;
22- @property (strong ) NSURL *blackListJSONURL ;
22+ @property (strong ) NSURL *jsonURL ;
2323@end
2424
25- @implementation BNCURLBlackList
25+ @implementation BNCURLFilter
2626
2727- (instancetype ) init {
2828 self = [super init ];
2929 if (!self) return self;
3030
31- self.blackList = @[
31+ self.patternList = @[
3232 @" ^fb\\ d+:" ,
3333 @" ^li\\ d+:" ,
3434 @" ^pdk\\ d+:" ,
@@ -37,16 +37,16 @@ - (instancetype) init {
3737 @" ^(?i)(?!(http|https):).*(:|:.*\\ b)(password|o?auth|o?auth.?token|access|access.?token)\\ b" ,
3838 @" ^(?i)((http|https):\\ /\\ /).*[\\ /|?|#].*\\ b(password|o?auth|o?auth.?token|access|access.?token)\\ b" ,
3939 ];
40- self.blackListVersion = -1 ; // First time always refresh the list version, version 0.
40+ self.listVersion = -1 ; // First time always refresh the list version, version 0.
4141
42- NSArray *storedList = [BNCPreferenceHelper preferenceHelper ].URLBlackList ;
42+ NSArray *storedList = [BNCPreferenceHelper preferenceHelper ].savedURLPatternList ;
4343 if (storedList.count > 0 ) {
44- self.blackList = storedList;
45- self.blackListVersion = [BNCPreferenceHelper preferenceHelper ].URLBlackListVersion ;
44+ self.patternList = storedList;
45+ self.listVersion = [BNCPreferenceHelper preferenceHelper ].savedURLPatternListVersion ;
4646 }
4747
4848 NSError *error = nil ;
49- _blackListRegex = [self .class compileRegexArray: self .blackList error: &error];
49+ _ignoredURLRegex = [self .class compileRegexArray: self .patternList error: &error];
5050 self.error = error;
5151
5252 return self;
@@ -57,24 +57,24 @@ - (void) dealloc {
5757 self.networkService = nil ;
5858}
5959
60- - (void ) setBlackList : (NSArray <NSString *> *)blackList {
60+ - (void ) setPatternList : (NSArray <NSString *> *)patternList {
6161 @synchronized (self) {
62- _blackList = blackList ;
63- _blackListRegex = [self .class compileRegexArray: _blackList error: nil ];
62+ _patternList = patternList ;
63+ _ignoredURLRegex = [self .class compileRegexArray: _patternList error: nil ];
6464 }
6565}
6666
67- - (NSArray <NSString*>*) blackList {
67+ - (NSArray <NSString*>*) patternList {
6868 @synchronized (self) {
69- return _blackList ;
69+ return _patternList ;
7070 }
7171}
7272
73- + (NSArray <NSRegularExpression*>*) compileRegexArray : (NSArray <NSString*>*)blacklist
73+ + (NSArray <NSRegularExpression*>*) compileRegexArray : (NSArray <NSString*>*)patternList
7474 error : (NSError *_Nullable __autoreleasing *_Nullable)error_ {
7575 if (error_) *error_ = nil ;
7676 NSMutableArray *array = [NSMutableArray new ];
77- for (NSString *pattern in blacklist ) {
77+ for (NSString *pattern in patternList ) {
7878 NSError *error = nil ;
7979 NSRegularExpression *regex =
8080 [NSRegularExpression regularExpressionWithPattern: pattern
@@ -90,40 +90,40 @@ - (void) setBlackList:(NSArray<NSString *> *)blackList {
9090 return array;
9191}
9292
93- - (NSString *_Nullable) blackListPatternMatchingURL : (NSURL *_Nullable)url {
93+ - (NSString *_Nullable) patternMatchingURL : (NSURL *_Nullable)url {
9494 NSString *urlString = url.absoluteString ;
9595 if (urlString == nil || urlString.length <= 0 ) return nil ;
9696 NSRange range = NSMakeRange (0 , urlString.length );
9797
98- for (NSRegularExpression * regex in self.blackListRegex ) {
98+ for (NSRegularExpression * regex in self.ignoredURLRegex ) {
9999 NSUInteger matches = [regex numberOfMatchesInString: urlString options: 0 range: range];
100100 if (matches > 0 ) return regex.pattern ;
101101 }
102102
103103 return nil ;
104104}
105105
106- - (BOOL ) isBlackListedURL : (NSURL *)url {
107- return ([self blackListPatternMatchingURL : url]) ? YES : NO ;
106+ - (BOOL ) shouldIgnoreURL : (NSURL *)url {
107+ return ([self patternMatchingURL : url]) ? YES : NO ;
108108}
109109
110- - (void ) refreshBlackListFromServer {
111- [self refreshBlackListFromServerWithCompletion :nil ];
110+ - (void ) updatePatternList {
111+ [self updatePatternListWithCompletion :nil ];
112112}
113113
114- - (void ) refreshBlackListFromServerWithCompletion : (void (^) (NSError *error, NSArray *list))completion {
114+ - (void ) updatePatternListWithCompletion : (void (^) (NSError *error, NSArray *list))completion {
115115 @synchronized (self) {
116- if (self.hasRefreshedBlackListFromServer ) {
117- if (completion) completion (self.error , self.blackList );
116+ if (self.hasUpdatedPatternList ) {
117+ if (completion) completion (self.error , self.patternList );
118118 return ;
119119 }
120- self.hasRefreshedBlackListFromServer = YES ;
120+ self.hasUpdatedPatternList = YES ;
121121 }
122122
123123 self.error = nil ;
124- NSString *urlString = [self .blackListJSONURL absoluteString ];
124+ NSString *urlString = [self .jsonURL absoluteString ];
125125 if (!urlString) {
126- urlString = [NSString stringWithFormat: @" %@ /sdk/uriskiplist_v%ld .json" , [BNCPreferenceHelper preferenceHelper ].branchBlacklistURL , (long ) self .blackListVersion +1 ];
126+ urlString = [NSString stringWithFormat: @" %@ /sdk/uriskiplist_v%ld .json" , [BNCPreferenceHelper preferenceHelper ].patternListURL , (long ) self .listVersion +1 ];
127127 }
128128 NSMutableURLRequest *request =
129129 [NSMutableURLRequest requestWithURL: [NSURL URLWithString: urlString]
@@ -135,7 +135,7 @@ - (void) refreshBlackListFromServerWithCompletion:(void (^) (NSError*error, NSAr
135135 [self .networkService networkOperationWithURLRequest: request completion:
136136 ^(id <BNCNetworkOperationProtocol> operation) {
137137 [self processServerOperation: operation];
138- if (completion) completion (self.error , self.blackList );
138+ if (completion) completion (self.error , self.patternList );
139139 [self .networkService cancelAllOperations ];
140140 self.networkService = nil ;
141141 }
@@ -149,9 +149,9 @@ - (void) processServerOperation:(id<BNCNetworkOperationProtocol>)operation {
149149 if (operation.responseData )
150150 responseString = [[NSString alloc ] initWithData: operation.responseData encoding: NSUTF8StringEncoding];
151151 if (operation.response .statusCode == 404 ) {
152- BNCLogDebugSDK (@" No new BlackList refresh found." );
152+ BNCLogDebugSDK (@" No new URL ignore list found." );
153153 } else {
154- BNCLogDebugSDK ([NSString stringWithFormat: @" BlackList refresh result. Error: %@ status: %ld body:\n %@ ." ,
154+ BNCLogDebugSDK ([NSString stringWithFormat: @" URL ignore list update result. Error: %@ status: %ld body:\n %@ ." ,
155155 operation.error, (long )operation.response.statusCode, responseString]);
156156 }
157157 if (operation.error || operation.responseData == nil || operation.response .statusCode != 200 ) {
@@ -166,16 +166,16 @@ - (void) processServerOperation:(id<BNCNetworkOperationProtocol>)operation {
166166 return ;
167167 }
168168
169- NSArray *blackListURLs = dictionary[@" uri_skip_list" ];
170- if (![blackListURLs isKindOfClass: NSArray .class]) return ;
169+ NSArray *urls = dictionary[@" uri_skip_list" ];
170+ if (![urls isKindOfClass: NSArray .class]) return ;
171171
172- NSNumber *blackListVersion = dictionary[@" version" ];
173- if (![blackListVersion isKindOfClass: NSNumber .class]) return ;
172+ NSNumber *version = dictionary[@" version" ];
173+ if (![version isKindOfClass: NSNumber .class]) return ;
174174
175- self.blackList = blackListURLs ;
176- self.blackListVersion = [blackListVersion longValue ];
177- [BNCPreferenceHelper preferenceHelper ].URLBlackList = self.blackList ;
178- [BNCPreferenceHelper preferenceHelper ].URLBlackListVersion = self.blackListVersion ;
175+ self.patternList = urls ;
176+ self.listVersion = [version longValue ];
177+ [BNCPreferenceHelper preferenceHelper ].savedURLPatternList = self.patternList ;
178+ [BNCPreferenceHelper preferenceHelper ].savedURLPatternListVersion = self.listVersion ;
179179}
180180
181181@end
0 commit comments