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 (
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) {