2015-05-04 12:04:17 -05:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
"github.com/mholt/caddy/middleware/headers"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Headers configures a new Headers middleware instance.
|
|
|
|
func Headers(c *Controller) (middleware.Middleware, error) {
|
|
|
|
rules, err := headersParse(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(next middleware.Handler) middleware.Handler {
|
|
|
|
return headers.Headers{Next: next, Rules: rules}
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func headersParse(c *Controller) ([]headers.Rule, error) {
|
|
|
|
var rules []headers.Rule
|
|
|
|
|
|
|
|
for c.NextLine() {
|
|
|
|
var head headers.Rule
|
|
|
|
var isNewPattern bool
|
|
|
|
|
|
|
|
if !c.NextArg() {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
pattern := c.Val()
|
|
|
|
|
2015-05-09 14:57:55 -05:00
|
|
|
// See if we already have a definition for this Path pattern...
|
2015-05-04 12:04:17 -05:00
|
|
|
for _, h := range rules {
|
2015-05-09 14:57:55 -05:00
|
|
|
if h.Path == pattern {
|
2015-05-04 12:04:17 -05:00
|
|
|
head = h
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...otherwise, this is a new pattern
|
2015-05-09 14:57:55 -05:00
|
|
|
if head.Path == "" {
|
|
|
|
head.Path = pattern
|
2015-05-04 12:04:17 -05:00
|
|
|
isNewPattern = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
// A block of headers was opened...
|
|
|
|
|
|
|
|
h := headers.Header{Name: c.Val()}
|
|
|
|
|
|
|
|
if c.NextArg() {
|
|
|
|
h.Value = c.Val()
|
|
|
|
}
|
|
|
|
|
|
|
|
head.Headers = append(head.Headers, h)
|
|
|
|
}
|
|
|
|
if c.NextArg() {
|
|
|
|
// ... or single header was defined as an argument instead.
|
|
|
|
|
|
|
|
h := headers.Header{Name: c.Val()}
|
|
|
|
|
|
|
|
h.Value = c.Val()
|
|
|
|
|
|
|
|
if c.NextArg() {
|
|
|
|
h.Value = c.Val()
|
|
|
|
}
|
|
|
|
|
|
|
|
head.Headers = append(head.Headers, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isNewPattern {
|
|
|
|
rules = append(rules, head)
|
|
|
|
} else {
|
|
|
|
for i := 0; i < len(rules); i++ {
|
2015-05-09 14:57:55 -05:00
|
|
|
if rules[i].Path == pattern {
|
2015-05-04 12:04:17 -05:00
|
|
|
rules[i] = head
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rules, nil
|
|
|
|
}
|