Skip to content

Commit

Permalink
fix: compression priority (#1950)
Browse files Browse the repository at this point in the history
* Initial update

* update remaining tests

* update .gitignore

* update another test, fix linting

* fix tests

* add missing Vary header
  • Loading branch information
inetol authored Feb 12, 2025
1 parent 243ce87 commit 8e25db0
Show file tree
Hide file tree
Showing 4 changed files with 453 additions and 129 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
tags
*.pprof
*.fasthttp.gz
*.fasthttp.br
*.fasthttp.zst
*.fasthttp.gz
.idea
.vscode
.DS_Store
Expand Down
24 changes: 19 additions & 5 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ var (
GenerateIndexPages: true,
Compress: true,
CompressBrotli: true,
CompressZstd: true,
AcceptByteRange: true,
}
rootFSHandler RequestHandler
Expand All @@ -156,6 +157,7 @@ func ServeFS(ctx *RequestCtx, filesystem fs.FS, path string) {
GenerateIndexPages: true,
Compress: true,
CompressBrotli: true,
CompressZstd: true,
AcceptByteRange: true,
}
handler := f.NewRequestHandler()
Expand Down Expand Up @@ -321,13 +323,20 @@ type FS struct {
// absolute paths on any filesystem.
AllowEmptyRoot bool

// Uses brotli encoding and fallbacks to gzip in responses if set to true, uses gzip if set to false.
// Uses brotli encoding and fallbacks to zstd or gzip in responses if set to true, uses zstd or gzip if set to false.
//
// This value has sense only if Compress is set.
//
// Brotli encoding is disabled by default.
CompressBrotli bool

// Uses zstd encoding and fallbacks to gzip in responses if set to true, uses gzip if set to false.
//
// This value has sense only if Compress is set.
//
// zstd encoding is disabled by default.
CompressZstd bool

// Index pages for directories without files matching IndexNames
// are automatically generated if set.
//
Expand Down Expand Up @@ -487,6 +496,7 @@ func (fs *FS) initRequestHandler() {
generateIndexPages: fs.GenerateIndexPages,
compress: fs.Compress,
compressBrotli: fs.CompressBrotli,
compressZstd: fs.CompressZstd,
compressRoot: compressRoot,
pathNotFound: fs.PathNotFound,
acceptByteRange: fs.AcceptByteRange,
Expand Down Expand Up @@ -518,6 +528,7 @@ type fsHandler struct {
generateIndexPages bool
compress bool
compressBrotli bool
compressZstd bool
acceptByteRange bool
}

Expand Down Expand Up @@ -1049,14 +1060,14 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
mustCompress = true
fileCacheKind = brotliCacheKind
fileEncoding = "br"
case h.compressZstd && ctx.Request.Header.HasAcceptEncodingBytes(strZstd):
mustCompress = true
fileCacheKind = zstdCacheKind
fileEncoding = "zstd"
case ctx.Request.Header.HasAcceptEncodingBytes(strGzip):
mustCompress = true
fileCacheKind = gzipCacheKind
fileEncoding = "gzip"
case ctx.Request.Header.HasAcceptEncodingBytes(strZstd):
mustCompress = true
fileCacheKind = zstdCacheKind
fileEncoding = "zstd"
}
}

Expand Down Expand Up @@ -1122,10 +1133,13 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
switch fileEncoding {
case "br":
hdr.SetContentEncodingBytes(strBr)
hdr.addVaryBytes(strAcceptEncoding)
case "gzip":
hdr.SetContentEncodingBytes(strGzip)
hdr.addVaryBytes(strAcceptEncoding)
case "zstd":
hdr.SetContentEncodingBytes(strZstd)
hdr.addVaryBytes(strAcceptEncoding)
}
}

Expand Down
Loading

0 comments on commit 8e25db0

Please sign in to comment.