Handler: support zstd encoding

This commit is contained in:
Laurence Withers 2026-06-06 11:10:44 +01:00
commit 5bece326fb

View file

@ -15,6 +15,7 @@ import (
const ( const (
encodingGzip = "gzip" encodingGzip = "gzip"
encodingBrotli = "br" encodingBrotli = "br"
encodingZstd = "zstd"
) )
// New returns a new handler. Standard security headers are set. // New returns a new handler. Standard security headers are set.
@ -148,11 +149,15 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// select compression // select compression
data := info.Uncompressed data := info.Uncompressed
gzip, brotli := acceptedEncodings(req) gzip, brotli, zstd := acceptedEncodings(req)
if brotli && info.Brotli != nil { switch {
case zstd && info.Zstd != nil:
data = info.Zstd
w.Header().Set("Content-Encoding", encodingZstd)
case brotli && info.Brotli != nil:
data = info.Brotli data = info.Brotli
w.Header().Set("Content-Encoding", encodingBrotli) w.Header().Set("Content-Encoding", encodingBrotli)
} else if gzip && info.Gzip != nil { case gzip && info.Gzip != nil:
data = info.Gzip data = info.Gzip
w.Header().Set("Content-Encoding", encodingGzip) w.Header().Set("Content-Encoding", encodingGzip)
} }
@ -182,7 +187,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Write(h.mapped[offset : offset+length]) w.Write(h.mapped[offset : offset+length])
} }
func acceptedEncodings(req *http.Request) (gzip, brotli bool) { func acceptedEncodings(req *http.Request) (gzip, brotli, zstd bool) {
encodings := req.Header.Get("Accept-Encoding") encodings := req.Header.Get("Accept-Encoding")
for _, enc := range strings.Split(encodings, ",") { for _, enc := range strings.Split(encodings, ",") {
switch strings.TrimSpace(enc) { switch strings.TrimSpace(enc) {