mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-24 06:26:17 +08:00
Moved gzip middleware into its own package
Trying a different format where the middleware is a type that satisfies http.Handler
This commit is contained in:
parent
affd470820
commit
80ef5d761c
|
@ -1,40 +0,0 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Gzip(p parser) Middleware {
|
||||
return func(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||
next(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
gzipWriter := gzip.NewWriter(w)
|
||||
defer gzipWriter.Close()
|
||||
gz := gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w}
|
||||
next(gz, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// gzipResponeWriter wraps the underlying Write method
|
||||
// with a gzip.Writer to compress the output.
|
||||
type gzipResponseWriter struct {
|
||||
io.Writer
|
||||
http.ResponseWriter
|
||||
}
|
||||
|
||||
// Write wraps the underlying Write method to do compression.
|
||||
func (w gzipResponseWriter) Write(b []byte) (int, error) {
|
||||
if w.Header().Get("Content-Type") == "" {
|
||||
w.Header().Set("Content-Type", http.DetectContentType(b))
|
||||
}
|
||||
n, err := w.Writer.Write(b)
|
||||
return n, err
|
||||
}
|
53
middleware/gzip/gzip.go
Normal file
53
middleware/gzip/gzip.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package gzip
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/mholt/caddy/middleware"
|
||||
)
|
||||
|
||||
// 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}
|
||||
return gz.ServeHTTP
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Gzip is a http.Handler middleware type which gzips HTTP responses.
|
||||
type Gzip struct {
|
||||
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) {
|
||||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||
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)
|
||||
}
|
||||
|
||||
// gzipResponeWriter wraps the underlying Write method
|
||||
// with a gzip.Writer to compress the output.
|
||||
type gzipResponseWriter struct {
|
||||
io.Writer
|
||||
http.ResponseWriter
|
||||
}
|
||||
|
||||
// Write wraps the underlying Write method to do compression.
|
||||
func (w gzipResponseWriter) Write(b []byte) (int, error) {
|
||||
if w.Header().Get("Content-Type") == "" {
|
||||
w.Header().Set("Content-Type", http.DetectContentType(b))
|
||||
}
|
||||
n, err := w.Writer.Write(b)
|
||||
return n, err
|
||||
}
|
Loading…
Reference in New Issue
Block a user