forked from AdguardTeam/urlfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
43 lines (36 loc) · 1.51 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package urlfilter
import (
"github.com/AdguardTeam/urlfilter/filterlist"
"github.com/AdguardTeam/urlfilter/rules"
)
// Engine represents the filtering engine with all the loaded rules
type Engine struct {
networkEngine *NetworkEngine
cosmeticEngine *CosmeticEngine
}
// NewEngine parses the filtering rules and creates a filtering engine of them
func NewEngine(s *filterlist.RuleStorage) *Engine {
return &Engine{
networkEngine: NewNetworkEngine(s),
cosmeticEngine: NewCosmeticEngine(s),
}
}
// MatchRequest - matches the specified request against the filtering engine
// and returns the matching result.
func (e *Engine) MatchRequest(r *rules.Request) (res *rules.MatchingResult) {
var networkRules []*rules.NetworkRule
var sourceRules []*rules.NetworkRule
networkRules = e.networkEngine.MatchAll(r)
if r.SourceURL != "" {
sourceRequest := rules.NewRequest(r.SourceURL, "", rules.TypeDocument)
sourceRules = e.networkEngine.MatchAll(sourceRequest)
}
return rules.NewMatchingResult(networkRules, sourceRules)
}
// GetCosmeticResult gets cosmetic result for the specified hostname and cosmetic options
func (e *Engine) GetCosmeticResult(hostname string, option rules.CosmeticOption) CosmeticResult {
includeCSS := option&rules.CosmeticOptionCSS == rules.CosmeticOptionCSS
includeGenericCSS := option&rules.CosmeticOptionGenericCSS == rules.CosmeticOptionGenericCSS
includeJS := option&rules.CosmeticOptionJS == rules.CosmeticOptionJS
return e.cosmeticEngine.Match(hostname, includeCSS, includeJS, includeGenericCSS)
}