10000 fix: compression priority (#1950) · valyala/fasthttp@8e25db0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e25db0

Browse files
authored
fix: compression priority (#1950)
* Initial update * update remaining tests * update .gitignore * update another test, fix linting * fix tests * add missing Vary header
1 parent 243ce87 commit 8e25db0

File tree

4 files changed

+453
-129
lines changed

4 files changed

+453
-129
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
tags
22
*.pprof
3-
*.fasthttp.gz
43
*.fasthttp.br
4+
*.fasthttp.zst
5+
*.fasthttp.gz
56
.idea
67
.vscode
78
.DS_Store

fs.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ var (
133133
GenerateIndexPages: true,
134134
Compress: true,
135135
CompressBrotli: true,
136+
CompressZstd: true,
136137
AcceptByteRange: true,
137138
}
138139
rootFSHandler RequestHandler
@@ -156,6 +157,7 @@ func ServeFS(ctx *RequestCtx, filesystem fs.FS, path string) {
156157
GenerateIndexPages: true,
157158
Compress: true,
158159
CompressBrotli: true,
160+
CompressZstd: true,
159161
AcceptByteRange: true,
160162
}
161163
handler := f.NewRequestHandler()
@@ -321,13 +323,20 @@ type FS struct {
321323
// absolute paths on any filesystem.
322324
AllowEmptyRoot bool
323325

324-
// Uses brotli encoding and fallbacks to gzip in responses if set to true, uses gzip if set to false.
326+
// Uses brotli encoding and fallbacks to zstd or gzip in responses if set to true, uses zstd or gzip if set to false.
325327
//
326328
// This value has sense only if Compress is set.
327329
//
328330
// Brotli encoding is disabled by default.
329331
CompressBrotli bool
330332

333+
// Uses zstd encoding and fallbacks to gzip in responses if set to true, uses gzip if set to false.
334+
//
335+
// This value has sense only if Compress is set.
336+
//
337+
// zstd encoding is disabled by default.
338+
CompressZstd bool
339+
331340
// Index pages for directories without files matching IndexNames
332341
// are automatically generated if set.
333342
//
@@ -487,6 +496,7 @@ func (fs *FS) initRequestHandler() {
487496
generateIndexPages: fs.GenerateIndexPages,
488497
compress: fs.Compress,
489498
compressBrotli: fs.CompressBrotli,
499+
compressZstd: fs.CompressZstd,
490500
compressRoot: compressRoot,
491501
pathNotFound: fs.PathNotFound,
492502
acceptByteRange: fs.AcceptByteRange,
@@ -518,6 +528,7 @@ type fsHandler struct {
518528
generateIndexPages bool
519529
compress bool
520530
compressBrotli bool
531+
compressZstd bool
521532
acceptByteRange bool
522533
}
523534

@@ -1049,14 +1060,14 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
10491060
mustCompress = true
10501061
fileCacheKind = brotliCacheKind
10511062
fileEncoding = "br"
1063+
case h.compressZstd && ctx.Request.Header.HasAcceptEncodingBytes(strZstd):
1064+
mustCompress = true
1065+
fileCacheKind = zstdCacheKind
1066+
fileEncoding = "zstd"
10521067
case ctx.Request.Header.HasAcceptEncodingBytes(strGzip):
10531068
mustCompress = true
10541069
fileCacheKind = gzipCacheKind
10551070
fileEncoding = "gzip"
1056-
case ctx.Request.Header.HasAcceptEncodingBytes(strZstd):
1057-
mustCompress = true
1058-
fileCacheKind = zstdCacheKind
1059-
fileEncoding = "zstd"
10601071
}
10611072
}
10621073

@@ -1122,10 +1133,13 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
11221133
switch fileEncoding {
11231134
case "br":
11241135
hdr.SetContentEncodingBytes(strBr)
1136+
hdr.addVaryBytes(strAcceptEncoding)
11251137
case "gzip":
11261138
hdr.SetContentEncodingBytes(strGzip)
1139+
hdr.addVaryBytes(strAcceptEncoding)
11271140
case "zstd":
11281141
hdr.SetContentEncodingBytes(strZstd)
1142+
hdr.addVaryBytes(strAcceptEncoding)
11291143
}
11301144
}
11311145

0 commit comments

Comments
 (0)
0