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