From dcc67863dc984896f147ce87b49b834ead504aab Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 29 Jan 2015 22:46:09 -0700 Subject: [PATCH] Experimenting to make middleware more independent --- middleware/gzip/gzip.go | 10 +++++----- middleware/headers/headers.go | 18 +++++++++--------- middleware/headers/new.go | 4 ++-- middleware/headers/parse.go | 10 +++++----- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/middleware/gzip/gzip.go b/middleware/gzip/gzip.go index ba71edbd..295496e1 100644 --- a/middleware/gzip/gzip.go +++ b/middleware/gzip/gzip.go @@ -12,28 +12,28 @@ import ( // New creates a new gzip middleware instance. func New(c middleware.Controller) (middleware.Middleware, error) { return func(next http.HandlerFunc) http.HandlerFunc { - gz := Gzip{next: next} + gz := Gzip{Next: next} return gz.ServeHTTP }, nil } // Gzip is a http.Handler middleware type which gzips HTTP responses. type Gzip struct { - next http.HandlerFunc + Next http.HandlerFunc // TODO: Compression level, other settings } // ServeHTTP serves a gzipped response if the client supports it. -func (g *Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) { if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { - g.next(w, r) + g.Next(w, r) return } w.Header().Set("Content-Encoding", "gzip") gzipWriter := gzip.NewWriter(w) defer gzipWriter.Close() gz := gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w} - g.next(gz, r) + g.Next(gz, r) } // gzipResponeWriter wraps the underlying Write method diff --git a/middleware/headers/headers.go b/middleware/headers/headers.go index dec6d83c..7faf6b64 100644 --- a/middleware/headers/headers.go +++ b/middleware/headers/headers.go @@ -9,33 +9,33 @@ import ( // Headers is middleware that adds headers to the responses // for requests matching a certain path. type Headers struct { - next http.HandlerFunc - rules []headers + Next http.HandlerFunc + Rules []HeaderRule } // 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 { +func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) { + for _, rule := range h.Rules { if middleware.Path(r.URL.Path).Matches(rule.Url) { for _, header := range rule.Headers { w.Header().Set(header.Name, header.Value) } } } - h.next(w, r) + h.Next(w, r) } type ( - // Headers 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?? - headers struct { + HeaderRule struct { Url string - Headers []header + Headers []Header } // Header represents a single HTTP header, simply a name and value. - header struct { + Header struct { Name string Value string } diff --git a/middleware/headers/new.go b/middleware/headers/new.go index fcb073a8..fd443463 100644 --- a/middleware/headers/new.go +++ b/middleware/headers/new.go @@ -16,8 +16,8 @@ func New(c middleware.Controller) (middleware.Middleware, error) { return func(next http.HandlerFunc) http.HandlerFunc { head := Headers{ - next: next, - rules: rules, + Next: next, + Rules: rules, } return head.ServeHTTP }, nil diff --git a/middleware/headers/parse.go b/middleware/headers/parse.go index 0c969bae..67f65701 100644 --- a/middleware/headers/parse.go +++ b/middleware/headers/parse.go @@ -2,11 +2,11 @@ package headers import "github.com/mholt/caddy/middleware" -func parse(c middleware.Controller) ([]headers, error) { - var rules []headers +func parse(c middleware.Controller) ([]HeaderRule, error) { + var rules []HeaderRule for c.NextLine() { - var head headers + var head HeaderRule var isNewPattern bool if !c.NextArg() { @@ -29,7 +29,7 @@ func parse(c middleware.Controller) ([]headers, error) { } for c.NextBlock() { - h := header{Name: c.Val()} + h := Header{Name: c.Val()} if c.NextArg() { h.Value = c.Val() @@ -38,7 +38,7 @@ func parse(c middleware.Controller) ([]headers, error) { head.Headers = append(head.Headers, h) } if c.NextArg() { - h := header{Name: c.Val()} + h := Header{Name: c.Val()} h.Value = c.Val()