From 5bece326fba7fb66dd3df72b571ca27c4ea5c586 Mon Sep 17 00:00:00 2001 From: Laurence Withers Date: Sat, 6 Jun 2026 11:10:44 +0100 Subject: [PATCH] Handler: support zstd encoding --- handler.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/handler.go b/handler.go index 5edffef..b61957e 100644 --- a/handler.go +++ b/handler.go @@ -15,6 +15,7 @@ import ( const ( encodingGzip = "gzip" encodingBrotli = "br" + encodingZstd = "zstd" ) // 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 data := info.Uncompressed - gzip, brotli := acceptedEncodings(req) - if brotli && info.Brotli != nil { + gzip, brotli, zstd := acceptedEncodings(req) + switch { + case zstd && info.Zstd != nil: + data = info.Zstd + w.Header().Set("Content-Encoding", encodingZstd) + case brotli && info.Brotli != nil: data = info.Brotli w.Header().Set("Content-Encoding", encodingBrotli) - } else if gzip && info.Gzip != nil { + case gzip && info.Gzip != nil: data = info.Gzip 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]) } -func acceptedEncodings(req *http.Request) (gzip, brotli bool) { +func acceptedEncodings(req *http.Request) (gzip, brotli, zstd bool) { encodings := req.Header.Get("Accept-Encoding") for _, enc := range strings.Split(encodings, ",") { switch strings.TrimSpace(enc) {