Commit dcc67863 authored by Matthew Holt's avatar Matthew Holt

Experimenting to make middleware more independent

parent ac7f50b4
......@@ -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
......
......@@ -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
}
......
......@@ -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
......
......@@ -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()
......
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