Commit 9d3151db authored by comp500's avatar comp500 Committed by Łukasz Nowak

Refactor header_upstream to use a fallthrough; return regex errors

parent 5087e403
...@@ -329,6 +329,8 @@ func parseUpstream(u string) ([]string, error) { ...@@ -329,6 +329,8 @@ func parseUpstream(u string) ([]string, error) {
} }
func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error { func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error {
var isUpstream bool
switch c.Val() { switch c.Val() {
case "policy": case "policy":
if !c.NextArg() { if !c.NextArg() {
...@@ -458,26 +460,8 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error { ...@@ -458,26 +460,8 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error {
} }
u.HealthCheck.ContentString = c.Val() u.HealthCheck.ContentString = c.Val()
case "header_upstream": case "header_upstream":
var header, value, replaced string isUpstream = true
if c.Args(&header, &value, &replaced) { fallthrough
// Don't allow - or + in replacements
if strings.HasPrefix(header, "-") || strings.HasPrefix(header, "+") {
return c.ArgErr()
}
r, err := regexp.Compile(value)
if err != nil {
return c.ArgErr()
}
u.upstreamHeaderReplacements.Add(header, headerReplacement{r, replaced})
} else {
if len(value) == 0 {
// When removing a header, the value can be optional.
if !strings.HasPrefix(header, "-") {
return c.ArgErr()
}
}
u.upstreamHeaders.Add(header, value)
}
case "header_downstream": case "header_downstream":
var header, value, replaced string var header, value, replaced string
if c.Args(&header, &value, &replaced) { if c.Args(&header, &value, &replaced) {
...@@ -487,9 +471,13 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error { ...@@ -487,9 +471,13 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error {
} }
r, err := regexp.Compile(value) r, err := regexp.Compile(value)
if err != nil { if err != nil {
return c.ArgErr() return err
}
if isUpstream {
u.upstreamHeaderReplacements.Add(header, headerReplacement{r, replaced})
} else {
u.downstreamHeaderReplacements.Add(header, headerReplacement{r, replaced})
} }
u.downstreamHeaderReplacements.Add(header, headerReplacement{r, replaced})
} else { } else {
if len(value) == 0 { if len(value) == 0 {
// When removing a header, the value can be optional. // When removing a header, the value can be optional.
...@@ -497,7 +485,11 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error { ...@@ -497,7 +485,11 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error {
return c.ArgErr() return c.ArgErr()
} }
} }
u.downstreamHeaders.Add(header, value) if isUpstream {
u.upstreamHeaders.Add(header, value)
} else {
u.downstreamHeaders.Add(header, value)
}
} }
case "transparent": case "transparent":
// Note: X-Forwarded-For header is always being appended for proxy connections // Note: X-Forwarded-For header is always being appended for proxy connections
......
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