Handler: support zstd encoding
This commit is contained in:
parent
0016a5e4fd
commit
5bece326fb
1 changed files with 9 additions and 4 deletions
13
handler.go
13
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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue