Commit 86b2d1aa authored by Geoff Simmons's avatar Geoff Simmons

DRY -- use only one CompareType in VCL spec types.

parent bfff5b5d
......@@ -454,6 +454,37 @@ func (worker *NamespaceWorker) configSharding(spec *vcl.Spec,
return nil
}
func configComparison(cmp vcr_v1alpha1.CompareType) (vcl.CompareType, bool) {
switch cmp {
case vcr_v1alpha1.Equal:
return vcl.Equal, false
case vcr_v1alpha1.NotEqual:
return vcl.Equal, true
case vcr_v1alpha1.Match:
return vcl.Match, false
case vcr_v1alpha1.NotMatch:
return vcl.Match, true
case vcr_v1alpha1.Prefix:
return vcl.Prefix, false
case vcr_v1alpha1.NotPrefix:
return vcl.Prefix, true
case vcr_v1alpha1.Exists:
return vcl.Exists, false
case vcr_v1alpha1.NotExists:
return vcl.Exists, true
case vcr_v1alpha1.Greater:
return vcl.Greater, false
case vcr_v1alpha1.GreaterEqual:
return vcl.GreaterEqual, false
case vcr_v1alpha1.Less:
return vcl.Less, false
case vcr_v1alpha1.LessEqual:
return vcl.LessEqual, false
default:
return vcl.Equal, false
}
}
func configConditions(vclConds []vcl.MatchTerm,
vcfgConds []vcr_v1alpha1.Condition) {
......@@ -465,18 +496,8 @@ func configConditions(vclConds []vcl.MatchTerm,
Comparand: cond.Comparand,
Value: cond.Value,
}
switch cond.Compare {
case vcr_v1alpha1.Equal:
vclMatch.Compare = vcl.Equal
case vcr_v1alpha1.NotEqual:
vclMatch.Compare = vcl.NotEqual
case vcr_v1alpha1.Match:
vclMatch.Compare = vcl.Match
case vcr_v1alpha1.NotMatch:
vclMatch.Compare = vcl.NotMatch
default:
vclMatch.Compare = vcl.Equal
}
vclMatch.Compare, vclMatch.Negate =
configComparison(cond.Compare)
vclConds[i] = vclMatch
}
}
......@@ -688,16 +709,10 @@ func (worker *NamespaceWorker) configRewrites(spec *vcl.Spec,
return fmt.Errorf("Illegal method %s", rw.Method)
}
switch rw.Compare {
case vcr_v1alpha1.Match:
vclRw.Compare = vcl.RewriteMatch
case vcr_v1alpha1.Equal:
vclRw.Compare = vcl.RewriteEqual
case vcr_v1alpha1.Prefix:
vclRw.Compare = vcl.Prefix
default:
vclRw.Compare = vcl.RewriteMatch
if rw.Compare == "" {
rw.Compare = vcr_v1alpha1.Match
}
vclRw.Compare, vclRw.Negate = configComparison(rw.Compare)
switch rw.VCLSub {
case vcr_v1alpha1.Recv:
......@@ -783,42 +798,8 @@ func (worker *NamespaceWorker) configReqDisps(spec *vcl.Spec,
count := uint(*cond.Count)
vclCond.Count = &count
}
switch cond.Compare {
case vcr_v1alpha1.Equal:
vclCond.Compare = vcl.ReqEqual
vclCond.Negate = false
case vcr_v1alpha1.NotEqual:
vclCond.Compare = vcl.ReqEqual
vclCond.Negate = true
case vcr_v1alpha1.Match:
vclCond.Compare = vcl.ReqMatch
vclCond.Negate = false
case vcr_v1alpha1.NotMatch:
vclCond.Compare = vcl.ReqMatch
vclCond.Negate = true
case vcr_v1alpha1.Prefix:
vclCond.Compare = vcl.ReqPrefix
vclCond.Negate = false
case vcr_v1alpha1.NotPrefix:
vclCond.Compare = vcl.ReqPrefix
vclCond.Negate = true
case vcr_v1alpha1.Exists:
vclCond.Compare = vcl.Exists
vclCond.Negate = false
case vcr_v1alpha1.NotExists:
vclCond.Compare = vcl.Exists
vclCond.Negate = true
case vcr_v1alpha1.Greater:
vclCond.Compare = vcl.Greater
case vcr_v1alpha1.GreaterEqual:
vclCond.Compare = vcl.GreaterEqual
case vcr_v1alpha1.Less:
vclCond.Compare = vcl.Less
case vcr_v1alpha1.LessEqual:
vclCond.Compare = vcl.LessEqual
default:
vclCond.Compare = vcl.ReqEqual
}
vclCond.Compare, vclCond.Negate =
configComparison(cond.Compare)
if cond.MatchFlags != nil {
vclCond.MatchFlags = configMatchFlags(
*cond.MatchFlags)
......
......@@ -315,7 +315,7 @@ var reqDispHarness = []struct {
Conditions: []vcl.Condition{
vcl.Condition{
Comparand: "req.method",
Compare: vcl.ReqEqual,
Compare: vcl.Equal,
Values: []string{"PRI"},
MatchFlags: vcl.MatchFlagsType{
CaseSensitive: true,
......@@ -339,7 +339,7 @@ var reqDispHarness = []struct {
},
vcl.Condition{
Comparand: "req.esi_level",
Compare: vcl.ReqEqual,
Compare: vcl.Equal,
Count: &uintZero,
MatchFlags: vcl.MatchFlagsType{
CaseSensitive: true,
......@@ -347,7 +347,7 @@ var reqDispHarness = []struct {
},
vcl.Condition{
Comparand: "req.proto",
Compare: vcl.ReqPrefix,
Compare: vcl.Prefix,
Values: []string{"HTTP/1.1"},
},
},
......@@ -360,7 +360,7 @@ var reqDispHarness = []struct {
Conditions: []vcl.Condition{
vcl.Condition{
Comparand: "req.method",
Compare: vcl.ReqEqual,
Compare: vcl.Equal,
Negate: true,
Values: []string{
"GET",
......@@ -385,7 +385,7 @@ var reqDispHarness = []struct {
Conditions: []vcl.Condition{
vcl.Condition{
Comparand: "req.method",
Compare: vcl.ReqEqual,
Compare: vcl.Equal,
Negate: true,
Values: []string{
"GET",
......@@ -471,7 +471,7 @@ var reqDispHarness = []struct {
Conditions: []vcl.Condition{
vcl.Condition{
Comparand: "req.method",
Compare: vcl.ReqEqual,
Compare: vcl.Equal,
Values: []string{"CONNECT"},
MatchFlags: vcl.MatchFlagsType{
CaseSensitive: true,
......@@ -486,7 +486,7 @@ var reqDispHarness = []struct {
Conditions: []vcl.Condition{
vcl.Condition{
Comparand: "req.method",
Compare: vcl.ReqEqual,
Compare: vcl.Equal,
Negate: true,
Values: []string{
"GET",
......@@ -551,7 +551,7 @@ var reqDispHarness = []struct {
Conditions: []vcl.Condition{
vcl.Condition{
Comparand: "req.url",
Compare: vcl.ReqMatch,
Compare: vcl.Match,
Values: []string{
`\.png$`,
`\.jpe?g$`,
......@@ -571,7 +571,7 @@ var reqDispHarness = []struct {
Conditions: []vcl.Condition{
vcl.Condition{
Comparand: "req.url",
Compare: vcl.ReqPrefix,
Compare: vcl.Prefix,
Values: []string{
"/interactive/",
"/basket/",
......@@ -608,7 +608,7 @@ var reqDispHarness = []struct {
Conditions: []vcl.Condition{
vcl.Condition{
Comparand: "req.method",
Compare: vcl.ReqEqual,
Compare: vcl.Equal,
Values: []string{"PURGE"},
MatchFlags: vcl.MatchFlagsType{
CaseSensitive: true,
......@@ -646,7 +646,7 @@ var reqDispHarness = []struct {
Conditions: []vcl.Condition{
vcl.Condition{
Comparand: "req.url",
Compare: vcl.ReqPrefix,
Compare: vcl.Prefix,
Negate: true,
Values: []string{
"/foo/",
......
......@@ -16,7 +16,7 @@ sub vcl_recv {
{{- range .ACLs}}
if (
{{- range $cond := .Conditions}}
{{$cond.Comparand}} {{cmpRelation .Compare}} "{{.Value}}" &&
{{$cond.Comparand}} {{cmpRelation .Compare .Negate}} "{{.Value}}" &&
{{- end}}
{{aclCmp .Comparand}} {{if .Whitelist}}!{{end}}~ {{aclName .Name}}
) {
......
......@@ -14,8 +14,8 @@ sub vcl_init {
sub vcl_recv {
{{- range .Auths}}
if (
{{- range $cond := .Conditions}}
{{$cond.Comparand}} {{cmpRelation .Compare}} "{{.Value}}" &&
{{- range .Conditions}}
{{.Comparand}} {{cmpRelation .Compare .Negate}} "{{.Value}}" &&
{{- end}}
{{- if eq .Status 401}}
!{{credsMatcher .Realm}}.match(req.http.Authorization)
......
......@@ -6,11 +6,11 @@ import selector;
{{range $cidx, $c := .Conditions -}}
{{if reqNeedsMatcher $c -}}
sub vcl_init {
new {{reqObj $didx $cidx}} = {{reqVMOD $c}}.set({{reqFlags $c}});
new {{reqObj $didx $cidx}} = {{vmod $c.Compare}}.set({{reqFlags $c}});
{{- range $val := $c.Values}}
{{reqObj $didx $cidx}}.add("{{$val}}");
{{- end}}
{{- if reqNeedsCompile $c}}
{{- if needsCompile $c.Compare}}
{{reqObj $didx $cidx}}.compile();
{{- end}}
}
......@@ -24,13 +24,13 @@ sub vcl_recv {
{{- range $cidx, $cond := .Conditions}}
{{- if ne $cidx 0}} &&
{{end}}
{{- if $cond.Negate}}! {{end}}
{{- if .Negate}}! {{end}}
{{- if reqNeedsMatcher $cond}}
{{- reqObj $didx $cidx}}.{{reqMatch $cond}}({{$cond.Comparand}})
{{- else if exists $cond.Compare}}
{{- $cond.Comparand}}
{{- reqObj $didx $cidx}}.{{match .Compare}}({{.Comparand}})
{{- else if exists .Compare}}
{{- .Comparand}}
{{- else}}
{{- $cond.Comparand}} {{reqCmpRelation $cond}} {{value $cond}}
{{- .Comparand}} {{cmpRelation .Compare .Negate}} {{value $cond}}
{{- end}}
{{- end -}}
) {
......
......@@ -37,7 +37,7 @@ var builtinRecvSpec = Spec{
{
Conditions: []Condition{{
Comparand: "req.method",
Compare: ReqEqual,
Compare: Equal,
Values: []string{"PRI"},
}},
Disposition: DispositionType{
......@@ -54,12 +54,12 @@ var builtinRecvSpec = Spec{
},
{
Comparand: "req.esi_level",
Compare: ReqEqual,
Compare: Equal,
Count: &zero,
},
{
Comparand: "req.proto",
Compare: ReqPrefix,
Compare: Prefix,
Values: []string{"HTTP/1.1"},
MatchFlags: MatchFlagsType{
CaseSensitive: false,
......@@ -74,7 +74,7 @@ var builtinRecvSpec = Spec{
{
Conditions: []Condition{{
Comparand: "req.method",
Compare: ReqEqual,
Compare: Equal,
Negate: true,
Values: []string{
"GET",
......@@ -96,7 +96,7 @@ var builtinRecvSpec = Spec{
{
Conditions: []Condition{{
Comparand: "req.method",
Compare: ReqEqual,
Compare: Equal,
Negate: true,
Values: []string{
"GET",
......@@ -140,7 +140,7 @@ var pipeOnConnectSpec = Spec{
Dispositions: []DispositionSpec{{
Conditions: []Condition{{
Comparand: "req.method",
Compare: ReqEqual,
Compare: Equal,
Values: []string{"CONNECT"},
}},
Disposition: DispositionType{
......@@ -158,7 +158,7 @@ var methodNotAllowedSpec = Spec{
Dispositions: []DispositionSpec{{
Conditions: []Condition{{
Comparand: "req.method",
Compare: ReqEqual,
Compare: Equal,
Negate: true,
Values: []string{
"GET",
......@@ -189,7 +189,7 @@ var urlWhitelistSpec = Spec{
Dispositions: []DispositionSpec{{
Conditions: []Condition{{
Comparand: "req.url",
Compare: ReqPrefix,
Compare: Prefix,
Negate: true,
Values: []string{
"/foo",
......@@ -217,7 +217,7 @@ var purgeMethodSpec = Spec{
Dispositions: []DispositionSpec{{
Conditions: []Condition{{
Comparand: "req.method",
Compare: ReqEqual,
Compare: Equal,
Values: []string{"PURGE"},
}},
Disposition: DispositionType{
......@@ -235,7 +235,7 @@ var cacheableSpec = Spec{
Dispositions: []DispositionSpec{{
Conditions: []Condition{{
Comparand: "req.url",
Compare: ReqMatch,
Compare: Match,
Values: []string{
`\.png$`,
`\.jpe?g$`,
......@@ -261,7 +261,7 @@ var nonCacheableSpec = Spec{
Dispositions: []DispositionSpec{{
Conditions: []Condition{{
Comparand: "req.url",
Compare: ReqPrefix,
Compare: Prefix,
Values: []string{
"/interactive/",
"/basket/",
......
......@@ -5,7 +5,7 @@ import selector;
{{range $i, $r := .Rewrites -}}
{{if needsMatcher $r -}}
sub vcl_init {
new {{rewrName $i}} = {{rewrVMOD $r}}.set({{rewrFlags $r}});
new {{rewrName $i}} = {{vmod $r.Compare}}.set({{rewrFlags $r}});
{{- range $rule := $r.Rules}}
{{rewrName $i}}.add("{{$rule.Value}}", string="{{$rule.Rewrite}}"
{{- if needsSave $r}}, save=true{{end -}}
......@@ -13,7 +13,7 @@ sub vcl_init {
{{- if needsNeverCapture $r}}, never_capture=true{{end -}}
);
{{- end}}
{{- if needsCompile $r}}
{{- if needsCompile $r.Compare}}
{{rewrName $i}}.compile();
{{- end}}
}
......@@ -21,7 +21,7 @@ sub vcl_init {
sub vcl_{{rewrSub $r}} {
{{- if needsMatcher $r}}
if ({{rewrName $i}}.{{rewrMatch $r}}({{$r.Source}})) {
if ({{rewrName $i}}.{{match $r.Compare}}({{$r.Source}})) {
{{- if needsUniqueCheck $r}}
if ({{rewrName $i}}.nmatches() != 1) {
std.log({{$r.Source}} + " had " +
......
......@@ -92,7 +92,7 @@ var replaceFromRewriteTest = Spec{
Rewrites: []Rewrite{{
Target: "req.http.Host",
Source: "req.http.Host",
Compare: RewriteEqual,
Compare: Equal,
Rules: []RewriteRule{
{
Value: "cafe.example.com",
......@@ -119,7 +119,7 @@ var rewriteSubTest = Spec{
Rewrites: []Rewrite{{
Target: "req.url",
Source: "req.url",
Compare: RewriteMatch,
Compare: Match,
Rules: []RewriteRule{
{
Value: `/foo(/|$)`,
......@@ -202,7 +202,7 @@ var conditionalDeleteTest = Spec{
Target: "resp.http.Via",
Source: "req.http.Delete-Via",
Method: Delete,
Compare: RewriteEqual,
Compare: Equal,
Rules: []RewriteRule{
{Value: "true"},
{Value: "yes"},
......@@ -222,9 +222,10 @@ func TestConditionalDelete(t *testing.T) {
var rewriteExtractTest = Spec{
Rewrites: []Rewrite{{
Target: "req.url",
Source: "req.url",
Method: RewriteMethod,
Target: "req.url",
Source: "req.url",
Method: RewriteMethod,
Compare: Match,
Rules: []RewriteRule{{
Value: `/([^/]+)/([^/]+)(.*)`,
Rewrite: `/\2/\1\3`,
......@@ -272,7 +273,7 @@ var rewriteFixedEqualTest = Spec{
Rewrites: []Rewrite{{
Target: "bereq.url",
Source: "bereq.url",
Compare: RewriteEqual,
Compare: Equal,
Method: Sub,
Rules: []RewriteRule{
{
......@@ -326,7 +327,7 @@ var rewritePrefixRegex = Spec{
Rewrites: []Rewrite{{
Target: "bereq.url",
Source: "bereq.url",
Compare: RewriteMatch,
Compare: Match,
Method: Sub,
Rules: []RewriteRule{
{
......@@ -355,7 +356,7 @@ var rewritePrependIfExists = Spec{
Rewrites: []Rewrite{{
Target: "resp.http.X-Bazz",
Source: "req.http.X-Bazz",
Compare: RewriteMatch,
Compare: Match,
Method: Prepend,
Rules: []RewriteRule{
{
......@@ -378,7 +379,7 @@ var rewriteExtractCookie = Spec{
Rewrites: []Rewrite{{
Target: "req.http.Session-Token",
Source: "req.http.Cookie",
Compare: RewriteMatch,
Compare: Match,
Method: RewriteMethod,
Rules: []RewriteRule{{
Value: `\bmysession\s*=\s*([^,;[:space:]]+)`,
......@@ -461,9 +462,10 @@ func TestRewriteAppendFromSrcTest(t *testing.T) {
var rewriteAppendRule = Spec{
Rewrites: []Rewrite{{
Target: "resp.http.Append-Rule-Target",
Source: "req.http.Append-Rule-Src",
Method: Append,
Target: "resp.http.Append-Rule-Target",
Source: "req.http.Append-Rule-Src",
Method: Append,
Compare: Match,
Rules: []RewriteRule{{
Value: `.`,
Rewrite: `AppendString`,
......@@ -649,10 +651,11 @@ var rewriteSelectOperations = Spec{
},
},
{
Target: "resp.http.Hdr",
Source: "req.url",
Select: First,
Method: Sub,
Target: "resp.http.Hdr",
Source: "req.url",
Compare: Match,
Select: First,
Method: Sub,
Rules: []RewriteRule{{
Value: `/foo`,
Rewrite: `foo`,
......@@ -662,10 +665,11 @@ var rewriteSelectOperations = Spec{
},
},
{
Target: "resp.http.Hdr",
Source: "req.url",
Select: Last,
Method: Suball,
Target: "resp.http.Hdr",
Source: "req.url",
Compare: Match,
Select: Last,
Method: Suball,
Rules: []RewriteRule{{
Value: `/foo`,
Rewrite: `foo`,
......@@ -675,10 +679,11 @@ var rewriteSelectOperations = Spec{
},
},
{
Target: "resp.http.Hdr",
Source: "req.url",
Select: First,
Method: RewriteMethod,
Target: "resp.http.Hdr",
Source: "req.url",
Compare: Match,
Select: First,
Method: RewriteMethod,
Rules: []RewriteRule{{
Value: `/foo`,
Rewrite: `foo`,
......
......@@ -315,19 +315,34 @@ func (a byACLAddr) Len() int { return len(a) }
func (a byACLAddr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byACLAddr) Less(i, j int) bool { return a[i].Addr < a[j].Addr }
// CompareType classifies comparisons for MatchTerm.
// CompareType classifies comparison operations performed for
// conditional configurations. The comparison may be negated if the
// boolean Negate field is true.
type CompareType uint8
const (
// Equal means compare strings for equality (==).
// Equal specifies equality -- string equality, numeric
// equality, or membership in a set of fixed strings.
Equal CompareType = iota
// NotEqual means compare with !=.
NotEqual
// Match means compare for regex match (~) -- the MatchTerm
// Value is a regular expression.
// Match specifies a regular expression match.
Match
// NotMatch means compare with !~.
NotMatch
// Prefix specifies that a string has a prefix in a set of
// fixed strings.
Prefix
// Exists specifies that a request header exists.
Exists
// Greater specifies the > relation between a VCL variable
// with a numeric value and a constant.
Greater
// GreaterEqual specifies the >= relation for a numeric VCL
// variable and a constant.
GreaterEqual
// Less specifies the < relation for a numeric VCL variable
// and a constant.
Less
// LessEqual specifies the <= relation for a numeric VCL
// variable and a constant.
LessEqual
)
// MatchTerm is a term describing the comparison of a VCL object with
......@@ -336,12 +351,18 @@ type MatchTerm struct {
Comparand string
Value string
Compare CompareType
Negate bool
}
func (match MatchTerm) hash(hash hash.Hash) {
hash.Write([]byte(match.Comparand))
hash.Write([]byte(match.Value))
hash.Write([]byte{byte(match.Compare)})
if match.Negate {
hash.Write([]byte{byte(1)})
} else {
hash.Write([]byte{byte(0)})
}
}
// interface for sorting []MatchTerm
......@@ -446,19 +467,6 @@ const (
Delete
)
// RewriteCompare classifies the comparison operation used to evaluate
// the conditions for a rewrite.
type RewriteCompare uint8
const (
// RewriteMatch means that a regex match is executed.
RewriteMatch RewriteCompare = iota
// RewriteEqual means that fixed strings are tested for equality.
RewriteEqual
// Prefix indicates a fixed-string prefix match.
Prefix
)
// SubType classifies the VCL subroutine in which a rewrite is
// executed.
type SubType uint8
......@@ -625,7 +633,8 @@ type Rewrite struct {
Target string
Source string
Method MethodType
Compare RewriteCompare
Compare CompareType
Negate bool
VCLSub SubType
Select SelectType
}
......@@ -640,6 +649,11 @@ func (rw Rewrite) hash(hash hash.Hash) {
hash.Write([]byte(rw.Source))
hash.Write([]byte{byte(rw.Method)})
hash.Write([]byte{byte(rw.Compare)})
if rw.Negate {
hash.Write([]byte{byte(1)})
} else {
hash.Write([]byte{byte(0)})
}
hash.Write([]byte{byte(rw.VCLSub)})
hash.Write([]byte{byte(rw.Select)})
}
......@@ -651,36 +665,6 @@ func (a byVCLSub) Len() int { return len(a) }
func (a byVCLSub) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byVCLSub) Less(i, j int) bool { return a[i].VCLSub < a[j].VCLSub }
// ReqCompareType classifies comparison operations performed for the
// Conditions in a request disposition specification. The relation may
// be negated, if the Negate field in Condition is true.
type ReqCompareType uint8
const (
// ReqEqual specifies equality -- string equality, numeric
// equality, or membership in a set of fixed strings.
ReqEqual ReqCompareType = iota
// ReqMatch specifies a regular expression match.
ReqMatch
// ReqPrefix specifies that a string has a prefix in a set of
// fixed strings.
ReqPrefix
// Exists specifies that a request header exists.
Exists
// Greater specifies the > relation between a VCL variable
// with a numeric value and a constant.
Greater
// GreaterEqual specifies the >= relation for a numeric VCL
// variable and a constant.
GreaterEqual
// Less specifies the < relation for a numeric VCL variable
// and a constant.
Less
// LessEqual specifies the <= relation for a numeric VCL
// variable and a constant.
LessEqual
)
// Condition specifies (one of) the conditions that must be true if a
// client request disposition is to be executed.
type Condition struct {
......@@ -688,7 +672,7 @@ type Condition struct {
MatchFlags MatchFlagsType
Count *uint
Comparand string
Compare ReqCompareType
Compare CompareType
Negate bool
}
......
......@@ -47,24 +47,22 @@ var fMap = template.FuncMap{
"dirType": func(svc Service) string { return dirType(svc) },
"rewrName": func(i int) string { return rewrName(i) },
"needsMatcher": func(rewr Rewrite) bool { return needsMatcher(rewr) },
"rewrVMOD": func(rewr Rewrite) string { return rewrVMOD(rewr) },
"rewrFlags": func(rewr Rewrite) string { return rewrFlags(rewr) },
"needsSave": func(rewr Rewrite) bool { return needsSave(rewr) },
"needsCompile": func(rewr Rewrite) bool { return needsCompile(rewr) },
"rewrSub": func(rewr Rewrite) string { return rewrSub(rewr) },
"rewrMatch": func(rewr Rewrite) string { return rewrMatch(rewr) },
"rewrOp": func(rewr Rewrite) string { return rewrOp(rewr) },
"rewrSelect": func(rewr Rewrite) string { return rewrSelect(rewr) },
"reqVMOD": func(cond Condition) string { return reqVMOD(cond) },
"reqMatch": func(cond Condition) string { return reqMatch(cond) },
"value": func(cond Condition) string { return reqValue(cond) },
"reqFlags": func(cond Condition) string { return reqFlags(cond) },
"exists": func(cmp ReqCompareType) bool { return cmp == Exists },
"needsCompile": func(cmp CompareType) bool { return cmp == Match },
"exists": func(cmp CompareType) bool { return cmp == Exists },
"match": func(cmp CompareType) string { return match(cmp) },
"vmod": func(cmp CompareType) string { return vmod(cmp) },
"aclCmp": func(comparand string) string {
return aclCmp(comparand)
},
"cmpRelation": func(cmp CompareType) string {
return cmpRelation(cmp)
"cmpRelation": func(cmp CompareType, negate bool) string {
return cmpRelation(cmp, negate)
},
"backendName": func(svc Service, addr string) string {
return backendName(svc, addr)
......@@ -97,7 +95,7 @@ var fMap = template.FuncMap{
return rewr.Method == Replace
},
"needsRegex": func(rewr Rewrite) bool {
return rewr.Compare != RewriteMatch &&
return rewr.Compare != Match &&
(rewr.Method == Sub || rewr.Method == Suball)
},
"saveRegex": func(rewr Rewrite, rule RewriteRule) string {
......@@ -108,11 +106,10 @@ var fMap = template.FuncMap{
return regex + "$"
},
"needsAll": func(rewr Rewrite) bool {
return rewr.Compare != RewriteMatch && rewr.Method == Suball
return rewr.Compare != Match && rewr.Method == Suball
},
"needsNeverCapture": func(rewr Rewrite) bool {
return rewr.Compare == RewriteMatch &&
rewr.MatchFlags.NeverCapture
return rewr.Compare == Match && rewr.MatchFlags.NeverCapture
},
"rewrOperand1": func(rewr Rewrite) string {
return rewrOperand1(rewr)
......@@ -121,7 +118,7 @@ var fMap = template.FuncMap{
return rewrOperand2(rewr, i)
},
"needsUniqueCheck": func(rewr Rewrite) bool {
if rewr.Compare == RewriteEqual || rewr.Select != Unique {
if rewr.Compare == Equal || rewr.Select != Unique {
return false
}
switch rewr.Method {
......@@ -142,12 +139,6 @@ var fMap = template.FuncMap{
"reqNeedsMatcher": func(cond Condition) bool {
return reqNeedsMatcher(cond)
},
"reqNeedsCompile": func(cond Condition) bool {
return reqNeedsCompile(cond)
},
"reqCmpRelation": func(cond Condition) string {
return reqCmpRelation(cond)
},
}
const (
......@@ -333,28 +324,18 @@ func hasXFF(acls []ACL) bool {
return false
}
func cmpRelation(cmp CompareType) string {
func cmpRelation(cmp CompareType, negate bool) string {
switch cmp {
case Equal:
return "=="
case NotEqual:
return "!="
case Match:
return "~"
case NotMatch:
return "!~"
default:
return "__INVALID_COMPARISON_TYPE__"
}
}
func reqCmpRelation(cond Condition) string {
switch cond.Compare {
case ReqEqual:
if cond.Negate {
if negate {
return "!="
}
return "=="
case Match:
if negate {
return "!~"
}
return "~"
case Greater:
return ">"
case GreaterEqual:
......@@ -390,7 +371,7 @@ func needsMatcher(rewr Rewrite) bool {
func reqNeedsMatcher(cond Condition) bool {
switch cond.Compare {
case ReqMatch, ReqPrefix:
case Match, Prefix:
return true
case Exists, Greater, GreaterEqual, Less, LessEqual:
return false
......@@ -405,11 +386,15 @@ func rewrName(i int) string {
return fmt.Sprintf("vk8s_rewrite_%d", i)
}
func rewrVMOD(rewr Rewrite) string {
if rewr.Compare == RewriteMatch {
func vmod(cmp CompareType) string {
switch cmp {
case Equal, Prefix:
return "selector"
case Match:
return "re2"
default:
return "__INVALID_COMPARISON_FOR_VMOD__"
}
return "selector"
}
func matcherFlags(isSelector bool, flagSpec MatchFlagsType) string {
......@@ -458,15 +443,15 @@ func matcherFlags(isSelector bool, flagSpec MatchFlagsType) string {
}
func rewrFlags(rewr Rewrite) string {
return matcherFlags(rewr.Compare != RewriteMatch, rewr.MatchFlags)
return matcherFlags(rewr.Compare != Match, rewr.MatchFlags)
}
func reqFlags(cond Condition) string {
return matcherFlags(cond.Compare != ReqMatch, cond.MatchFlags)
return matcherFlags(cond.Compare != Match, cond.MatchFlags)
}
func needsSave(rewr Rewrite) bool {
if rewr.Compare != RewriteMatch {
if rewr.Compare != Match {
return false
}
switch rewr.Method {
......@@ -477,14 +462,6 @@ func needsSave(rewr Rewrite) bool {
}
}
func needsCompile(rewr Rewrite) bool {
return rewr.Compare == RewriteMatch
}
func reqNeedsCompile(cond Condition) bool {
return cond.Compare == ReqMatch
}
func rewrSub(rewr Rewrite) string {
if rewr.VCLSub == Unspecified {
if strings.HasPrefix(rewr.Target, "resp") ||
......@@ -553,10 +530,9 @@ func rewrOperand2(rewr Rewrite, i int) string {
return rewr.Source
}
// XXX DRY
func rewrMatch(rewr Rewrite) string {
switch rewr.Compare {
case RewriteMatch, RewriteEqual:
func match(cmp CompareType) string {
switch cmp {
case Match, Equal:
return "match"
case Prefix:
return "hasprefix"
......@@ -565,23 +541,12 @@ func rewrMatch(rewr Rewrite) string {
}
}
func reqMatch(cond Condition) string {
switch cond.Compare {
case ReqMatch, ReqEqual:
return "match"
case ReqPrefix:
return "hasprefix"
default:
return "__INVALID_MATCH_OPERATION__"
}
}
func rewrOp(rewr Rewrite) string {
switch rewr.Method {
case Sub:
return "sub"
case Suball:
if rewr.Compare == RewriteMatch {
if rewr.Compare == Match {
return "suball"
}
return "sub"
......@@ -592,17 +557,6 @@ func rewrOp(rewr Rewrite) string {
}
}
func reqVMOD(cond Condition) string {
switch cond.Compare {
case ReqEqual, ReqPrefix:
return "selector"
case ReqMatch:
return "re2"
default:
return "__INVALID_COMPARISON_FOR_VMOD__"
}
}
func reqValue(cond Condition) string {
if cond.Count != nil {
return fmt.Sprintf("%d", *cond.Count)
......
......@@ -377,12 +377,14 @@ var acls = Spec{
Conditions: []MatchTerm{
MatchTerm{
Comparand: "req.http.Host",
Compare: NotEqual,
Compare: Equal,
Negate: true,
Value: "cafe.example.com",
},
MatchTerm{
Comparand: "req.url",
Compare: NotMatch,
Compare: Match,
Negate: true,
Value: `^/tea(/|$)`,
},
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment