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