Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions plugin/rewrite/cname_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ type cnameTargetRule struct {
paramFromTarget string
paramToTarget string
nextAction string
Upstream UpstreamInt // Upstream for looking up external names during the resolution process.
pattern *regexp.Regexp // Compiled paramFromTarget regex for RegexMatch
Upstream UpstreamInt // Upstream for looking up external names during the resolution process.
}

// cnameTargetRuleWithReqState is cname target rewrite rule state
Expand All @@ -52,8 +53,7 @@ func (r *cnameTargetRule) getFromAndToTarget(inputCName string) (from string, to
return inputCName, strings.ReplaceAll(inputCName, r.paramFromTarget, r.paramToTarget)
}
case RegexMatch:
pattern := regexp.MustCompile(r.paramFromTarget)
regexGroups := pattern.FindStringSubmatch(inputCName)
regexGroups := r.pattern.FindStringSubmatch(inputCName)
if len(regexGroups) == 0 {
return "", ""
}
Expand Down Expand Up @@ -143,6 +143,13 @@ func newCNAMERule(nextAction string, args ...string) (Rule, error) {
nextAction: nextAction,
Upstream: upstream.New(),
}
if rewriteType == RegexMatch {
re, err := regexp.Compile(paramFromTarget)
if err != nil {
return nil, fmt.Errorf("invalid cname rewrite regex pattern: %w", err)
}
rule.pattern = re
}
return &rule, nil
}

Expand Down
1 change: 1 addition & 0 deletions plugin/rewrite/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestParse(t *testing.T) {
}`, true, "must begin with a name rule"},
{`rewrite stop`, true, ""},
{`rewrite continue`, true, ""},
{`rewrite stop name regex [bad[ bar answer name bar foo`, true, ""},
}

for i, test := range tests {
Expand Down