mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-09 09:36:58 +01:00
Refactored headers middleware to return errors
This commit is contained in:
parent
8f4e7f7fdc
commit
a39e71ca26
2 changed files with 7 additions and 11 deletions
|
@ -12,13 +12,13 @@ import (
|
||||||
// Headers is middleware that adds headers to the responses
|
// Headers is middleware that adds headers to the responses
|
||||||
// for requests matching a certain path.
|
// for requests matching a certain path.
|
||||||
type Headers struct {
|
type Headers struct {
|
||||||
Next http.HandlerFunc
|
Next middleware.HandlerFunc
|
||||||
Rules []HeaderRule
|
Rules []HeaderRule
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP implements the http.Handler interface and serves the requests,
|
// ServeHTTP implements the middleware.Handler interface and serves requests,
|
||||||
// adding headers to the response according to the configured rules.
|
// adding headers to the response according to the configured rules.
|
||||||
func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
for _, rule := range h.Rules {
|
for _, rule := range h.Rules {
|
||||||
if middleware.Path(r.URL.Path).Matches(rule.Url) {
|
if middleware.Path(r.URL.Path).Matches(rule.Url) {
|
||||||
for _, header := range rule.Headers {
|
for _, header := range rule.Headers {
|
||||||
|
@ -26,12 +26,12 @@ func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
h.Next(w, r)
|
return h.Next(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// HeaderRule groups a slice of HTTP headers by a URL pattern.
|
// HeaderRule groups a slice of HTTP headers by a URL pattern.
|
||||||
// TODO: use http.Header type instead??
|
// TODO: use http.Header type instead?
|
||||||
HeaderRule struct {
|
HeaderRule struct {
|
||||||
Url string
|
Url string
|
||||||
Headers []Header
|
Headers []Header
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
package headers
|
package headers
|
||||||
|
|
||||||
import (
|
import "github.com/mholt/caddy/middleware"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/mholt/caddy/middleware"
|
|
||||||
)
|
|
||||||
|
|
||||||
// New constructs and configures a new headers middleware instance.
|
// New constructs and configures a new headers middleware instance.
|
||||||
func New(c middleware.Controller) (middleware.Middleware, error) {
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
|
@ -14,7 +10,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(next http.HandlerFunc) http.HandlerFunc {
|
return func(next middleware.HandlerFunc) middleware.HandlerFunc {
|
||||||
return Headers{Next: next, Rules: rules}.ServeHTTP
|
return Headers{Next: next, Rules: rules}.ServeHTTP
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue