Commit 1e4baa53 authored by Matthew Holt's avatar Matthew Holt

Moved headers middleware into its own package

Further trying out spreading out the code outside of the nested functions
parent 80ef5d76
package headers
import (
"net/http"
"github.com/mholt/caddy/middleware/util"
)
// Headers is middleware that adds headers to the responses
// for requests matching a certain path.
type Headers struct {
next http.HandlerFunc
rules []headers
}
// ServeHTTP implements the http.Handler interface and serves the requests,
// adding headers to the response according to the configured rules.
func (h *Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, rule := range h.rules {
if util.Path(r.URL.Path).Matches(rule.Url) {
for _, header := range rule.Headers {
w.Header().Set(header.Name, header.Value)
}
}
}
h.next(w, r)
}
type (
// Headers groups a slice of HTTP headers by a URL pattern.
// TODO: use http.Header type instead??
headers struct {
Url string
Headers []header
}
// Header represents a single HTTP header, simply a name and value.
header struct {
Name string
Value string
}
)
package headers
import (
"net/http"
"github.com/mholt/caddy/middleware"
)
// New constructs and configures a new headers middleware instance.
func New(c middleware.Controller) (middleware.Middleware, error) {
rules, err := parse(c)
if err != nil {
return nil, err
}
return func(next http.HandlerFunc) http.HandlerFunc {
head := Headers{
next: next,
rules: rules,
}
return head.ServeHTTP
}, nil
}
package middleware package headers
import "net/http" import "github.com/mholt/caddy/middleware"
// Headers is middleware that adds headers to the responses func parse(c middleware.Controller) ([]headers, error) {
// for requests matching a certain path.
func Headers(p parser) Middleware {
type (
// Header represents a single HTTP header, simply a name and value.
header struct {
Name string
Value string
}
// Headers groups a slice of HTTP headers by a URL pattern.
headers struct {
Url string
Headers []header
}
)
var rules []headers var rules []headers
for p.NextLine() { for c.NextLine() {
var head headers var head headers
var isNewPattern bool var isNewPattern bool
if !p.NextArg() { if !c.NextArg() {
return p.ArgErr() return rules, c.ArgErr()
} }
pattern := p.Val() pattern := c.Val()
// See if we already have a definition for this URL pattern... // See if we already have a definition for this URL pattern...
for _, h := range rules { for _, h := range rules {
...@@ -43,22 +28,22 @@ func Headers(p parser) Middleware { ...@@ -43,22 +28,22 @@ func Headers(p parser) Middleware {
isNewPattern = true isNewPattern = true
} }
for p.NextBlock() { for c.NextBlock() {
h := header{Name: p.Val()} h := header{Name: c.Val()}
if p.NextArg() { if c.NextArg() {
h.Value = p.Val() h.Value = c.Val()
} }
head.Headers = append(head.Headers, h) head.Headers = append(head.Headers, h)
} }
if p.NextArg() { if c.NextArg() {
h := header{Name: p.Val()} h := header{Name: c.Val()}
h.Value = p.Val() h.Value = c.Val()
if p.NextArg() { if c.NextArg() {
h.Value = p.Val() h.Value = c.Val()
} }
head.Headers = append(head.Headers, h) head.Headers = append(head.Headers, h)
...@@ -76,16 +61,5 @@ func Headers(p parser) Middleware { ...@@ -76,16 +61,5 @@ func Headers(p parser) Middleware {
} }
} }
return func(next http.HandlerFunc) http.HandlerFunc { return rules, nil
return func(w http.ResponseWriter, r *http.Request) {
for _, rule := range rules {
if Path(r.URL.Path).Matches(rule.Url) {
for _, header := range rule.Headers {
w.Header().Set(header.Name, header.Value)
}
}
}
next(w, r)
}
}
} }
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