mirror of
https://github.com/mjl-/mox.git
synced 2025-07-10 07:54:40 +03:00
implement transparent gzip compression in the webserver
we only compress if applicable (content-type indicates likely compressible), client supports it, response doesn't already have a content-encoding). for internal handlers, we always enable compression. for reverse proxied and static files, compression must be enabled per handler. for internal & reverse proxy handlers, we do streaming compression at "bestspeed" quality (probably level 1). for static files, we have a cache based on mtime with fixed max size, where we evict based on least recently used. we compress with the default level (more cpu, better ratio).
This commit is contained in:
@ -224,6 +224,7 @@ func (m *merged) serve(ctx context.Context, log *mlog.Log, w http.ResponseWriter
|
||||
gz := acceptsGzip(r)
|
||||
var out []byte
|
||||
var mtime time.Time
|
||||
var origSize int64
|
||||
|
||||
func() {
|
||||
m.Lock()
|
||||
@ -265,13 +266,14 @@ func (m *merged) serve(ctx context.Context, log *mlog.Log, w http.ResponseWriter
|
||||
xcheckf(ctx, err, "gzipping combined html")
|
||||
m.combinedGzip = b.Bytes()
|
||||
}
|
||||
origSize = int64(len(out))
|
||||
out = m.combinedGzip
|
||||
}
|
||||
mtime = m.mtime
|
||||
}()
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
http.ServeContent(gzipInjector{w, gz}, r, "", mtime, bytes.NewReader(out))
|
||||
http.ServeContent(gzipInjector{w, gz, origSize}, r, "", mtime, bytes.NewReader(out))
|
||||
}
|
||||
|
||||
// gzipInjector is a http.ResponseWriter that optionally injects a
|
||||
@ -283,6 +285,7 @@ func (m *merged) serve(ctx context.Context, log *mlog.Log, w http.ResponseWriter
|
||||
type gzipInjector struct {
|
||||
http.ResponseWriter // Keep most methods.
|
||||
gz bool
|
||||
origSize int64
|
||||
}
|
||||
|
||||
// WriteHeader adds a Content-Encoding: gzip header before actually writing the
|
||||
@ -290,6 +293,9 @@ type gzipInjector struct {
|
||||
func (w gzipInjector) WriteHeader(statusCode int) {
|
||||
if w.gz && statusCode == http.StatusOK {
|
||||
w.ResponseWriter.Header().Set("Content-Encoding", "gzip")
|
||||
if lw, ok := w.ResponseWriter.(interface{ SetUncompressedSize(int64) }); ok {
|
||||
lw.SetUncompressedSize(w.origSize)
|
||||
}
|
||||
}
|
||||
w.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
Reference in New Issue
Block a user