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
// 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
}
)
func parse(c middleware.Controller) ([]headers, error) {
var rules []headers
for p.NextLine() {
for c.NextLine() {
var head headers
var isNewPattern bool
if !p.NextArg() {
return p.ArgErr()
if !c.NextArg() {
return rules, c.ArgErr()
}
pattern := p.Val()
pattern := c.Val()
// See if we already have a definition for this URL pattern...
for _, h := range rules {
......@@ -43,22 +28,22 @@ func Headers(p parser) Middleware {
isNewPattern = true
}
for p.NextBlock() {
h := header{Name: p.Val()}
for c.NextBlock() {
h := header{Name: c.Val()}
if p.NextArg() {
h.Value = p.Val()
if c.NextArg() {
h.Value = c.Val()
}
head.Headers = append(head.Headers, h)
}
if p.NextArg() {
h := header{Name: p.Val()}
if c.NextArg() {
h := header{Name: c.Val()}
h.Value = p.Val()
h.Value = c.Val()
if p.NextArg() {
h.Value = p.Val()
if c.NextArg() {
h.Value = c.Val()
}
head.Headers = append(head.Headers, h)
......@@ -76,16 +61,5 @@ func Headers(p parser) Middleware {
}
}
return func(next http.HandlerFunc) http.HandlerFunc {
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)
}
}
return rules, nil
}
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