Commit dcc67863 authored by Matthew Holt's avatar Matthew Holt

Experimenting to make middleware more independent

parent ac7f50b4
...@@ -12,28 +12,28 @@ import ( ...@@ -12,28 +12,28 @@ import (
// New creates a new gzip middleware instance. // New creates a new gzip middleware instance.
func New(c middleware.Controller) (middleware.Middleware, error) { func New(c middleware.Controller) (middleware.Middleware, error) {
return func(next http.HandlerFunc) http.HandlerFunc { return func(next http.HandlerFunc) http.HandlerFunc {
gz := Gzip{next: next} gz := Gzip{Next: next}
return gz.ServeHTTP return gz.ServeHTTP
}, nil }, nil
} }
// Gzip is a http.Handler middleware type which gzips HTTP responses. // Gzip is a http.Handler middleware type which gzips HTTP responses.
type Gzip struct { type Gzip struct {
next http.HandlerFunc Next http.HandlerFunc
// TODO: Compression level, other settings // TODO: Compression level, other settings
} }
// ServeHTTP serves a gzipped response if the client supports it. // 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") { if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
g.next(w, r) g.Next(w, r)
return return
} }
w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Encoding", "gzip")
gzipWriter := gzip.NewWriter(w) gzipWriter := gzip.NewWriter(w)
defer gzipWriter.Close() defer gzipWriter.Close()
gz := gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w} gz := gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w}
g.next(gz, r) g.Next(gz, r)
} }
// gzipResponeWriter wraps the underlying Write method // gzipResponeWriter wraps the underlying Write method
......
...@@ -9,33 +9,33 @@ import ( ...@@ -9,33 +9,33 @@ 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 http.HandlerFunc
rules []headers Rules []HeaderRule
} }
// ServeHTTP implements the http.Handler interface and serves the requests, // ServeHTTP implements the http.Handler interface and serves the 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) {
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 {
w.Header().Set(header.Name, header.Value) w.Header().Set(header.Name, header.Value)
} }
} }
} }
h.next(w, r) h.Next(w, r)
} }
type ( 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?? // TODO: use http.Header type instead??
headers struct { HeaderRule struct {
Url string Url string
Headers []header Headers []Header
} }
// Header represents a single HTTP header, simply a name and value. // Header represents a single HTTP header, simply a name and value.
header struct { Header struct {
Name string Name string
Value string Value string
} }
......
...@@ -16,8 +16,8 @@ func New(c middleware.Controller) (middleware.Middleware, error) { ...@@ -16,8 +16,8 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
return func(next http.HandlerFunc) http.HandlerFunc { return func(next http.HandlerFunc) http.HandlerFunc {
head := Headers{ head := Headers{
next: next, Next: next,
rules: rules, Rules: rules,
} }
return head.ServeHTTP return head.ServeHTTP
}, nil }, nil
......
...@@ -2,11 +2,11 @@ package headers ...@@ -2,11 +2,11 @@ package headers
import "github.com/mholt/caddy/middleware" import "github.com/mholt/caddy/middleware"
func parse(c middleware.Controller) ([]headers, error) { func parse(c middleware.Controller) ([]HeaderRule, error) {
var rules []headers var rules []HeaderRule
for c.NextLine() { for c.NextLine() {
var head headers var head HeaderRule
var isNewPattern bool var isNewPattern bool
if !c.NextArg() { if !c.NextArg() {
...@@ -29,7 +29,7 @@ func parse(c middleware.Controller) ([]headers, error) { ...@@ -29,7 +29,7 @@ func parse(c middleware.Controller) ([]headers, error) {
} }
for c.NextBlock() { for c.NextBlock() {
h := header{Name: c.Val()} h := Header{Name: c.Val()}
if c.NextArg() { if c.NextArg() {
h.Value = c.Val() h.Value = c.Val()
...@@ -38,7 +38,7 @@ func parse(c middleware.Controller) ([]headers, error) { ...@@ -38,7 +38,7 @@ func parse(c middleware.Controller) ([]headers, error) {
head.Headers = append(head.Headers, h) head.Headers = append(head.Headers, h)
} }
if c.NextArg() { if c.NextArg() {
h := header{Name: c.Val()} h := Header{Name: c.Val()}
h.Value = c.Val() h.Value = c.Val()
......
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