@@ -8,15 +8,11 @@ use std::ops::DerefMut;
8
8
use std:: sync:: Arc ;
9
9
use thiserror:: Error ;
10
10
11
- #[ cfg( feature = "object-pooling" ) ]
12
- use lifeguard:: Pool ;
13
-
14
11
use crate :: filters:: network:: { NetworkFilter , NetworkFilterMaskHelper , NetworkMatchable } ;
15
12
use crate :: optimizer;
16
13
use crate :: regex_manager:: { RegexManager , RegexManagerDiscardPolicy } ;
17
14
use crate :: request:: Request ;
18
15
use crate :: resources:: ResourceStorage ;
19
- use crate :: utils;
20
16
use crate :: utils:: { fast_hash, Hash } ;
21
17
22
18
/// Options used when constructing a [`Blocker`].
@@ -88,25 +84,6 @@ pub enum BlockerError {
88
84
FilterExists ,
89
85
}
90
86
91
- #[ cfg( feature = "object-pooling" ) ]
92
- pub ( crate ) struct TokenPool {
93
- pub pool : Pool < Vec < utils:: Hash > > ,
94
- }
95
-
96
- #[ cfg( feature = "object-pooling" ) ]
97
- impl Default for TokenPool {
98
- fn default ( ) -> TokenPool {
99
- TokenPool {
100
- pool : lifeguard:: pool ( )
101
- . with ( lifeguard:: StartingSize ( 1 ) )
102
- . with ( lifeguard:: Supplier ( || {
103
- Vec :: with_capacity ( utils:: TOKENS_BUFFER_SIZE )
104
- } ) )
105
- . build ( ) ,
106
- }
107
- }
108
- }
109
-
110
87
// only check for tags in tagged and exception rule buckets,
111
88
// pass empty set for the rest
112
89
static NO_TAGS : Lazy < HashSet < String > > = Lazy :: new ( HashSet :: new) ;
@@ -129,10 +106,6 @@ pub struct Blocker {
129
106
130
107
pub ( crate ) enable_optimizations : bool ,
131
108
132
- // Not serialized
133
- #[ cfg( feature = "object-pooling" ) ]
134
- pub ( crate ) pool : TokenPool ,
135
-
136
109
// Not serialized
137
110
#[ cfg( feature = "unsync-regex-caching" ) ]
138
111
pub ( crate ) regex_manager : std:: cell:: RefCell < RegexManager > ,
@@ -167,24 +140,8 @@ impl Blocker {
167
140
168
141
pub fn check_generic_hide ( & self , hostname_request : & Request ) -> bool {
169
142
let mut regex_manager = self . borrow_regex_manager ( ) ;
170
- let mut request_tokens;
171
- #[ cfg( feature = "object-pooling" ) ]
172
- {
173
- request_tokens = self . pool . pool . new ( ) ;
174
- }
175
- #[ cfg( not( feature = "object-pooling" ) ) ]
176
- {
177
- request_tokens = Vec :: with_capacity ( utils:: TOKENS_BUFFER_SIZE ) ;
178
- }
179
- hostname_request. get_tokens ( & mut request_tokens) ;
180
-
181
143
self . generic_hide
182
- . check (
183
- hostname_request,
184
- & request_tokens,
185
- & HashSet :: new ( ) ,
186
- & mut regex_manager,
187
- )
144
+ . check ( hostname_request, & HashSet :: new ( ) , & mut regex_manager)
188
145
. is_some ( )
189
146
}
190
147
@@ -200,70 +157,41 @@ impl Blocker {
200
157
return BlockerResult :: default ( ) ;
201
158
}
202
159
203
- let mut request_tokens;
204
- #[ cfg( feature = "object-pooling" ) ]
205
- {
206
- request_tokens = self . pool . pool . new ( ) ;
207
- }
208
- #[ cfg( not( feature = "object-pooling" ) ) ]
209
- {
210
- request_tokens = Vec :: with_capacity ( utils:: TOKENS_BUFFER_SIZE ) ;
211
- }
212
- request. get_tokens ( & mut request_tokens) ;
213
-
214
160
// Check the filters in the following order:
215
161
// 1. $important (not subject to exceptions)
216
162
// 2. redirection ($redirect=resource)
217
163
// 3. normal filters - if no match by then
218
164
// 4. exceptions - if any non-important match of forced
219
165
220
166
// Always check important filters
221
- let important_filter =
222
- self . importants
223
- . check ( request, & request_tokens, & NO_TAGS , & mut regex_manager) ;
167
+ let important_filter = self . importants . check ( request, & NO_TAGS , & mut regex_manager) ;
224
168
225
169
// only check the rest of the rules if not previously matched
226
170
let filter = if important_filter. is_none ( ) && !matched_rule {
227
171
self . filters_tagged
228
- . check (
229
- request,
230
- & request_tokens,
231
- & self . tags_enabled ,
232
- & mut regex_manager,
233
- )
234
- . or_else ( || {
235
- self . filters
236
- . check ( request, & request_tokens, & NO_TAGS , & mut regex_manager)
237
- } )
172
+ . check ( request, & self . tags_enabled , & mut regex_manager)
173
+ . or_else ( || self . filters . check ( request, & NO_TAGS , & mut regex_manager) )
238
174
} else {
239
175
important_filter
240
176
} ;
241
177
242
178
let exception = match filter. as_ref ( ) {
243
179
// if no other rule matches, only check exceptions if forced to
244
- None if matched_rule || force_check_exceptions => self . exceptions . check (
245
- request,
246
- & request_tokens,
247
- & self . tags_enabled ,
248
- & mut regex_manager,
249
- ) ,
180
+ None if matched_rule || force_check_exceptions => {
181
+ self . exceptions
182
+ . check ( request, & self . tags_enabled , & mut regex_manager)
183
+ }
250
184
None => None ,
251
185
// If matched an important filter, exceptions don't atter
252
186
Some ( f) if f. is_important ( ) => None ,
253
- Some ( _) => self . exceptions . check (
254
- request,
255
- & request_tokens,
256
- & self . tags_enabled ,
257
- & mut regex_manager,
258
- ) ,
187
+ Some ( _) => self
188
+ . exceptions
189
+ . check ( request, & self . tags_enabled , & mut regex_manager) ,
259
190
} ;
260
191
261
- let redirect_filters = self . redirects . check_all (
262
- request,
263
- & request_tokens,
264
- & NO_TAGS ,
265
- regex_manager. deref_mut ( ) ,
266
- ) ;
192
+ let redirect_filters =
193
+ self . redirects
194
+ . check_all ( request, & NO_TAGS , regex_manager. deref_mut ( ) ) ;
267
195
268
196
// Extract the highest priority redirect directive.
269
197
// 1. Exceptions - can bail immediately if found
@@ -328,12 +256,7 @@ impl Blocker {
328
256
let rewritten_url = if important {
329
257
None
330
258
} else {
331
- Self :: apply_removeparam (
332
- & self . removeparam ,
333
- request,
334
- & request_tokens,
335
- regex_manager. deref_mut ( ) ,
336
- )
259
+ Self :: apply_removeparam ( & self . removeparam , request, regex_manager. deref_mut ( ) )
337
260
} ;
338
261
339
262
// If something has already matched before but we don't know what, still return a match
@@ -351,7 +274,6 @@ impl Blocker {
351
274
fn apply_removeparam (
352
275
removeparam_filters : & NetworkFilterList ,
353
276
request : & Request ,
354
- request_tokens : & [ Hash ] ,
355
277
regex_manager : & mut RegexManager ,
356
278
) -> Option < String > {
357
279
/// Represents an `&`-separated argument from a URL query parameter string
@@ -395,8 +317,7 @@ impl Blocker {
395
317
. map ( |param| ( param, true ) )
396
318
. collect ( ) ;
397
319
398
- let filters =
399
- removeparam_filters. check_all ( request, request_tokens, & NO_TAGS , regex_manager) ;
320
+ let filters = removeparam_filters. check_all ( request, & NO_TAGS , regex_manager) ;
400
321
let mut rewrite = false ;
401
322
for removeparam_filter in filters {
402
323
if let Some ( removeparam) = & removeparam_filter. modifier_option {
@@ -448,25 +369,10 @@ impl Blocker {
448
369
return None ;
449
370
}
450
371
451
- let mut request_tokens;
452
372
let mut regex_manager = self . borrow_regex_manager ( ) ;
453
-
454
- #[ cfg( feature = "object-pooling" ) ]
455
- {
456
- request_tokens = self . pool . pool . new ( ) ;
457
- }
458
- #[ cfg( not( feature = "object-pooling" ) ) ]
459
- {
460
- request_tokens = Vec :: with_capacity ( utils:: TOKENS_BUFFER_SIZE ) ;
461
- }
462
- request. get_tokens ( & mut request_tokens) ;
463
-
464
- let filters = self . csp . check_all (
465
- request,
466
- & request_tokens,
467
- & self . tags_enabled ,
468
- & mut regex_manager,
469
- ) ;
373
+ let filters = self
374
+ . csp
375
+ . check_all ( request, & self . tags_enabled , & mut regex_manager) ;
470
376
471
377
if filters. is_empty ( ) {
472
378
return None ;
@@ -597,9 +503,6 @@ impl Blocker {
597
503
tagged_filters_all,
598
504
// Options
599
505
enable_optimizations : options. enable_optimizations ,
600
-
601
- #[ cfg( feature = "object-pooling" ) ]
602
- pool : TokenPool :: default ( ) ,
603
506
regex_manager : Default :: default ( ) ,
604
507
}
605
508
}
@@ -888,34 +791,14 @@ impl NetworkFilterList {
888
791
pub fn check (
889
792
& self ,
890
793
request : & Request ,
891
- request_tokens : & [ Hash ] ,
892
794
active_tags : & HashSet < String > ,
893
795
regex_manager : & mut RegexManager ,
894
796
) -> Option < & NetworkFilter > {
895
797
if self . filter_map . is_empty ( ) {
896
798
return None ;
897
799
}
898
800
899
- if let Some ( source_hostname_hashes) = request. source_hostname_hashes . as_ref ( ) {
900
- for token in source_hostname_hashes {
901
- if let Some ( filter_bucket) = self . filter_map . get ( token) {
902
- for filter in filter_bucket {
903
- // if matched, also needs to be tagged with an active tag (or not tagged at all)
904
- if filter. matches ( request, regex_manager)
905
- && filter
906
- . tag
907
- . as_ref ( )
908
- . map ( |t| active_tags. contains ( t) )
909
- . unwrap_or ( true )
910
- {
911
- return Some ( filter) ;
912
- }
913
- }
914
- }
915
- }
916
- }
917
-
918
- for token in request_tokens {
801
+ for token in request. get_tokens_for_match ( ) {
919
802
if let Some ( filter_bucket) = self . filter_map . get ( token) {
920
803
for filter in filter_bucket {
921
804
// if matched, also needs to be tagged with an active tag (or not tagged at all)
@@ -942,7 +825,6 @@ impl NetworkFilterList {
942
825
pub fn check_all (
943
826
& self ,
944
827
request : & Request ,
945
- request_tokens : & [ Hash ] ,
946
828
active_tags : & HashSet < String > ,
947
829
regex_manager : & mut RegexManager ,
948
830
) -> Vec < & NetworkFilter > {
@@ -952,26 +834,7 @@ impl NetworkFilterList {
952
834
return filters;
953
835
}
954
836
955
- if let Some ( source_hostname_hashes) = request. source_hostname_hashes . as_ref ( ) {
956
- for token in source_hostname_hashes {
957
- if let Some ( filter_bucket) = self . filter_map . get ( token) {
958
- for filter in filter_bucket {
959
- // if matched, also needs to be tagged with an active tag (or not tagged at all)
960
- if filter. matches ( request, regex_manager)
961
- && filter
962
- . tag
963
- . as_ref ( )
964
- . map ( |t| active_tags. contains ( t) )
965
- . unwrap_or ( true )
966
- {
967
- filters. push ( filter) ;
968
- }
969
- }
970
- }
971
- }
972
- }
973
-
974
- for token in request_tokens {
837
+ for token in request. get_tokens_for_match ( ) {
975
838
if let Some ( filter_bucket) = self . filter_map . get ( token) {
976
839
for filter in filter_bucket {
977
840
// if matched, also needs to be tagged with an active tag (or not tagged at all)
@@ -987,7 +850,6 @@ impl NetworkFilterList {
987
850
}
988
851
}
989
852
}
990
-
991
853
filters
992
854
}
993
855
}
0 commit comments