Skip to content

Commit 2c67299

Browse files
authored
fix: location matcher logic (#7)
1 parent b59398e commit 2c67299

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
.PHONY: build
12
build:
23
go build -o ./bin/go-ngx-config ./cmd/go-ngx-config/*.go
34

5+
.PHONY: build-wasm
46
build-wasm:
57
GOOS=js GOARCH=wasm go build -o ./bin/go-ngx-config-parser.wasm ./cmd/go-ngx-config-wasm/main.go
68

9+
.PHONY: clean-wasm-dev
710
clean-wasm-dev:
811
rm ./examples/wasm/wasm_exec.js || true
912
rm ./examples/wasm/go-ngx-config-parser.wasm || true
1013

11-
prepare-wasm-dev: clean-wasm-dev build-wasm
14+
.PHONY: serve-wasm-dev
15+
serve-wasm-dev: clean-wasm-dev build-wasm
1216
bash -c "cp /usr/local/Cellar/go@1.14/1.14.15/libexec/misc/wasm/wasm_exec.js ./examples/wasm/"
13-
@cp ./bin/go-ngx-config-parser.wasm ./examples/wasm/
17+
@cp ./bin/go-ngx-config-parser.wasm ./examples/wasm/
18+
pnpm serve --filter=wasm-example

internal/matcher/location.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func NewLocationMatcher(conf *ast.Config, targetPath string) (*LocationMatcher,
7474
}
7575

7676
func locationTester(locationsTarget []ast.Location, targetPath string) (*LocationMatcher, error) {
77+
// handle exact
7778
for _, location := range locationsTarget {
7879
if location.Modifier != EXACT {
7980
continue
@@ -88,9 +89,9 @@ func locationTester(locationsTarget []ast.Location, targetPath string) (*Locatio
8889
}
8990
}
9091

91-
var bestMatch *ast.Location
92+
// handle prefix and prefix priority
93+
var bestMatch ast.Location
9294
bestLength := 0
93-
9495
for _, location := range locationsTarget {
9596
if location.Modifier != PREFIX && location.Modifier != PREFIX_PRIORITY {
9697
continue
@@ -99,12 +100,22 @@ func locationTester(locationsTarget []ast.Location, targetPath string) (*Locatio
99100
if strings.HasPrefix(targetPath, location.Match) {
100101
locationLength := len(location.Match)
101102
if locationLength > bestLength {
102-
bestMatch = &location
103+
bestMatch = location
103104
bestLength = locationLength
104105
}
105106
}
106107
}
107108

109+
// do not go to regex if priority
110+
if bestMatch.Match != "" && bestMatch.Modifier == PREFIX_PRIORITY {
111+
return &LocationMatcher{
112+
MatchPath: bestMatch.Match,
113+
MatchModifer: bestMatch.Modifier,
114+
Directives: bestMatch.Directives,
115+
}, nil
116+
}
117+
118+
// handle regex
108119
for _, location := range locationsTarget {
109120
if location.Modifier == REGEX || location.Modifier == REGEX_NO_CASE_SENSITIVE {
110121
locationRegex := location.Match
@@ -128,7 +139,8 @@ func locationTester(locationsTarget []ast.Location, targetPath string) (*Locatio
128139
}
129140
}
130141

131-
if bestMatch != nil {
142+
// use longest match
143+
if bestMatch.Match != "" {
132144
return &LocationMatcher{
133145
MatchPath: bestMatch.Match,
134146
MatchModifer: bestMatch.Modifier,

0 commit comments

Comments
 (0)