aboutsummaryrefslogtreecommitdiffhomepage
path: root/modules/caddyhttp/reverseproxy/selectionpolicies.go
diff options
context:
space:
mode:
authorFrancis Lavoie <[email protected]>2024-01-23 19:36:59 -0500
committerGitHub <[email protected]>2024-01-23 19:36:59 -0500
commit750d0b83319ac0ea6b7f057b8270c19404c3d6fa (patch)
treed0c3fb610cde8ae9d73a0a2caba94542af099770 /modules/caddyhttp/reverseproxy/selectionpolicies.go
parent54823f52bc9aed66a1a37f820daf6e494181211a (diff)
downloadcaddy-750d0b83319ac0ea6b7f057b8270c19404c3d6fa.tar.gz
caddy-750d0b83319ac0ea6b7f057b8270c19404c3d6fa.zip
caddyfile: Normalize & flatten all unmarshalers (#6037)
Diffstat (limited to 'modules/caddyhttp/reverseproxy/selectionpolicies.go')
-rw-r--r--modules/caddyhttp/reverseproxy/selectionpolicies.go125
1 files changed, 60 insertions, 65 deletions
diff --git a/modules/caddyhttp/reverseproxy/selectionpolicies.go b/modules/caddyhttp/reverseproxy/selectionpolicies.go
index acb069a74..b56c8074c 100644
--- a/modules/caddyhttp/reverseproxy/selectionpolicies.go
+++ b/modules/caddyhttp/reverseproxy/selectionpolicies.go
@@ -68,10 +68,9 @@ func (r RandomSelection) Select(pool UpstreamPool, request *http.Request, _ http
// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (r *RandomSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- for d.Next() {
- if d.NextArg() {
- return d.ArgErr()
- }
+ d.Next() // consume policy name
+ if d.NextArg() {
+ return d.ArgErr()
}
return nil
}
@@ -98,22 +97,22 @@ func (WeightedRoundRobinSelection) CaddyModule() caddy.ModuleInfo {
// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (r *WeightedRoundRobinSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- for d.Next() {
- args := d.RemainingArgs()
- if len(args) == 0 {
- return d.ArgErr()
- }
+ d.Next() // consume policy name
- for _, weight := range args {
- weightInt, err := strconv.Atoi(weight)
- if err != nil {
- return d.Errf("invalid weight value '%s': %v", weight, err)
- }
- if weightInt < 1 {
- return d.Errf("invalid weight value '%s': weight should be non-zero and positive", weight)
- }
- r.Weights = append(r.Weights, weightInt)
+ args := d.RemainingArgs()
+ if len(args) == 0 {
+ return d.ArgErr()
+ }
+
+ for _, weight := range args {
+ weightInt, err := strconv.Atoi(weight)
+ if err != nil {
+ return d.Errf("invalid weight value '%s': %v", weight, err)
+ }
+ if weightInt < 1 {
+ return d.Errf("invalid weight value '%s': weight should be non-zero and positive", weight)
}
+ r.Weights = append(r.Weights, weightInt)
}
return nil
}
@@ -179,17 +178,17 @@ func (RandomChoiceSelection) CaddyModule() caddy.ModuleInfo {
// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (r *RandomChoiceSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- for d.Next() {
- if !d.NextArg() {
- return d.ArgErr()
- }
- chooseStr := d.Val()
- choose, err := strconv.Atoi(chooseStr)
- if err != nil {
- return d.Errf("invalid choice value '%s': %v", chooseStr, err)
- }
- r.Choose = choose
+ d.Next() // consume policy name
+
+ if !d.NextArg() {
+ return d.ArgErr()
}
+ chooseStr := d.Val()
+ choose, err := strconv.Atoi(chooseStr)
+ if err != nil {
+ return d.Errf("invalid choice value '%s': %v", chooseStr, err)
+ }
+ r.Choose = choose
return nil
}
@@ -280,10 +279,9 @@ func (LeastConnSelection) Select(pool UpstreamPool, _ *http.Request, _ http.Resp
// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (r *LeastConnSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- for d.Next() {
- if d.NextArg() {
- return d.ArgErr()
- }
+ d.Next() // consume policy name
+ if d.NextArg() {
+ return d.ArgErr()
}
return nil
}
@@ -320,10 +318,9 @@ func (r *RoundRobinSelection) Select(pool UpstreamPool, _ *http.Request, _ http.
// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (r *RoundRobinSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- for d.Next() {
- if d.NextArg() {
- return d.ArgErr()
- }
+ d.Next() // consume policy name
+ if d.NextArg() {
+ return d.ArgErr()
}
return nil
}
@@ -352,10 +349,9 @@ func (FirstSelection) Select(pool UpstreamPool, _ *http.Request, _ http.Response
// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (r *FirstSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- for d.Next() {
- if d.NextArg() {
- return d.ArgErr()
- }
+ d.Next() // consume policy name
+ if d.NextArg() {
+ return d.ArgErr()
}
return nil
}
@@ -383,10 +379,9 @@ func (IPHashSelection) Select(pool UpstreamPool, req *http.Request, _ http.Respo
// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (r *IPHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- for d.Next() {
- if d.NextArg() {
- return d.ArgErr()
- }
+ d.Next() // consume policy name
+ if d.NextArg() {
+ return d.ArgErr()
}
return nil
}
@@ -416,10 +411,9 @@ func (ClientIPHashSelection) Select(pool UpstreamPool, req *http.Request, _ http
// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (r *ClientIPHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- for d.Next() {
- if d.NextArg() {
- return d.ArgErr()
- }
+ d.Next() // consume policy name
+ if d.NextArg() {
+ return d.ArgErr()
}
return nil
}
@@ -443,10 +437,9 @@ func (URIHashSelection) Select(pool UpstreamPool, req *http.Request, _ http.Resp
// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (r *URIHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- for d.Next() {
- if d.NextArg() {
- return d.ArgErr()
- }
+ d.Next() // consume policy name
+ if d.NextArg() {
+ return d.ArgErr()
}
return nil
}
@@ -504,13 +497,14 @@ func (s QueryHashSelection) Select(pool UpstreamPool, req *http.Request, _ http.
// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (s *QueryHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- for d.Next() {
- if !d.NextArg() {
- return d.ArgErr()
- }
- s.Key = d.Val()
+ d.Next() // consume policy name
+
+ if !d.NextArg() {
+ return d.ArgErr()
}
- for nesting := d.Nesting(); d.NextBlock(nesting); {
+ s.Key = d.Val()
+
+ for d.NextBlock(0) {
switch d.Val() {
case "fallback":
if !d.NextArg() {
@@ -583,13 +577,14 @@ func (s HeaderHashSelection) Select(pool UpstreamPool, req *http.Request, _ http
// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (s *HeaderHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- for d.Next() {
- if !d.NextArg() {
- return d.ArgErr()
- }
- s.Field = d.Val()
+ d.Next() // consume policy name
+
+ if !d.NextArg() {
+ return d.ArgErr()
}
- for nesting := d.Nesting(); d.NextBlock(nesting); {
+ s.Field = d.Val()
+
+ for d.NextBlock(0) {
switch d.Val() {
case "fallback":
if !d.NextArg() {
@@ -708,7 +703,7 @@ func (s *CookieHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
default:
return d.ArgErr()
}
- for nesting := d.Nesting(); d.NextBlock(nesting); {
+ for d.NextBlock(0) {
switch d.Val() {
case "fallback":
if !d.NextArg() {