diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8cbf1fe738..5cfc4e5edb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,7 @@ jobs: strategy: fail-fast: false matrix: - go-version: [1.19.x, 1.20.x, 1.21.x] + go-version: [1.19.x, 1.20.x, 1.21.x, 1.22.x] os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: diff --git a/fs.go b/fs.go index c2ea512292..4f2bbbf606 100644 --- a/fs.go +++ b/fs.go @@ -1038,15 +1038,16 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) { fileEncoding := "" byteRange := ctx.Request.Header.peek(strRange) if len(byteRange) == 0 && h.compress { - if h.compressBrotli && ctx.Request.Header.HasAcceptEncodingBytes(strBr) { + switch { + case h.compressBrotli && ctx.Request.Header.HasAcceptEncodingBytes(strBr): mustCompress = true fileCacheKind = brotliCacheKind fileEncoding = "br" - } else if ctx.Request.Header.HasAcceptEncodingBytes(strGzip) { + case ctx.Request.Header.HasAcceptEncodingBytes(strGzip): mustCompress = true fileCacheKind = gzipCacheKind fileEncoding = "gzip" - } else if ctx.Request.Header.HasAcceptEncodingBytes(strZstd) { + case ctx.Request.Header.HasAcceptEncodingBytes(strZstd): mustCompress = true fileCacheKind = zstdCacheKind fileEncoding = "zstd" @@ -1107,11 +1108,12 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) { hdr := &ctx.Response.Header if ff.compressed { - if fileEncoding == "br" { + switch fileEncoding { + case "br": hdr.SetContentEncodingBytes(strBr) - } else if fileEncoding == "gzip" { + case "gzip": hdr.SetContentEncodingBytes(strGzip) - } else if fileEncoding == "zstd" { + case "zstd": hdr.SetContentEncodingBytes(strZstd) } } @@ -1316,11 +1318,12 @@ nestedContinue: if mustCompress { var zbuf bytebufferpool.ByteBuffer - if fileEncoding == "br" { + switch fileEncoding { + case "br": zbuf.B = AppendBrotliBytesLevel(zbuf.B, w.B, CompressDefaultCompression) - } else if fileEncoding == "gzip" { + case "gzip": zbuf.B = AppendGzipBytesLevel(zbuf.B, w.B, CompressDefaultCompression) - } else if fileEncoding == "zstd" { + case "zstd": zbuf.B = AppendZstdBytesLevel(zbuf.B, w.B, CompressZstdDefault) } w = &zbuf @@ -1420,21 +1423,22 @@ func (h *fsHandler) compressFileNolock( } return nil, errNoCreatePermission } - if fileEncoding == "br" { + switch fileEncoding { + case "br": zw := acquireStacklessBrotliWriter(zf, CompressDefaultCompression) _, err = copyZeroAlloc(zw, f) if err1 := zw.Flush(); err == nil { err = err1 } releaseStacklessBrotliWriter(zw, CompressDefaultCompression) - } else if fileEncoding == "gzip" { + case "gzip": zw := acquireStacklessGzipWriter(zf, CompressDefaultCompression) _, err = copyZeroAlloc(zw, f) if err1 := zw.Flush(); err == nil { err = err1 } releaseStacklessGzipWriter(zw, CompressDefaultCompression) - } else if fileEncoding == "zstd" { + case "zstd": zw := acquireStacklessZstdWriter(zf, CompressZstdDefault) _, err = copyZeroAlloc(zw, f) if err1 := zw.Flush(); err == nil { @@ -1464,21 +1468,22 @@ func (h *fsHandler) newCompressedFSFileCache(f fs.File, fileInfo fs.FileInfo, fi err error ) - if fileEncoding == "br" { + switch fileEncoding { + case "br": zw := acquireStacklessBrotliWriter(w, CompressDefaultCompression) _, err = copyZeroAlloc(zw, f) if err1 := zw.Flush(); err == nil { err = err1 } releaseStacklessBrotliWriter(zw, CompressDefaultCompression) - } else if fileEncoding == "gzip" { + case "gzip": zw := acquireStacklessGzipWriter(w, CompressDefaultCompression) _, err = copyZeroAlloc(zw, f) if err1 := zw.Flush(); err == nil { err = err1 } releaseStacklessGzipWriter(zw, CompressDefaultCompression) - } else if fileEncoding == "zstd" { + case "zstd": zw := acquireStacklessZstdWriter(w, CompressZstdDefault) _, err = copyZeroAlloc(zw, f) if err1 := zw.Flush(); err == nil { @@ -1634,17 +1639,18 @@ func readFileHeader(f io.Reader, compressed bool, fileEncoding string) ([]byte, ) if compressed { var err error - if fileEncoding == "br" { + switch fileEncoding { + case "br": if br, err = acquireBrotliReader(f); err != nil { return nil, err } r = br - } else if fileEncoding == "gzip" { + case "gzip": if zr, err = acquireGzipReader(f); err != nil { return nil, err } r = zr - } else if fileEncoding == "zstd" { + case "zstd": if zsr, err = acquireZstdReader(f); err != nil { return nil, err } diff --git a/server.go b/server.go index bdfbcc6d66..426351b383 100644 --- a/server.go +++ b/server.go @@ -523,11 +523,12 @@ func CompressHandler(h RequestHandler) RequestHandler { func CompressHandlerLevel(h RequestHandler, level int) RequestHandler { return func(ctx *RequestCtx) { h(ctx) - if ctx.Request.Header.HasAcceptEncodingBytes(strGzip) { + switch { + case ctx.Request.Header.HasAcceptEncodingBytes(strGzip): ctx.Response.gzipBody(level) //nolint:errcheck - } else if ctx.Request.Header.HasAcceptEncodingBytes(strDeflate) { + case ctx.Request.Header.HasAcceptEncodingBytes(strDeflate): ctx.Response.deflateBody(level) //nolint:errcheck - } else if ctx.Request.Header.HasAcceptEncodingBytes(strZstd) { + case ctx.Request.Header.HasAcceptEncodingBytes(strZstd): ctx.Response.zstdBody(level) //nolint:errcheck } } diff --git a/zstd.go b/zstd.go index 15ab2463d0..226a126326 100644 --- a/zstd.go +++ b/zstd.go @@ -8,7 +8,6 @@ import ( "github.com/klauspost/compress/zstd" "github.com/valyala/bytebufferpool" - "github.com/valyala/fasthttp/stackless" ) diff --git a/zstd_test.go b/zstd_test.go index 620ef7d708..dc0c45f339 100644 --- a/zstd_test.go +++ b/zstd_test.go @@ -38,7 +38,7 @@ func testZstdBytesSingleCase(s string) error { if !bytes.Equal(ZstdpedS[:len(prefix)], prefix) { return fmt.Errorf("unexpected prefix when compressing %q: %q. Expecting %q", s, ZstdpedS[:len(prefix)], prefix) } - + unZstdedS, err := AppendUnzstdBytes(prefix, ZstdpedS[len(prefix):]) if err != nil { return fmt.Errorf("unexpected error when uncompressing %q: %w", s, err)