Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: simplify readFileHeader by using defer statements #1726

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ tags
*.pprof
*.fasthttp.gz
*.fasthttp.br
*.fasthttp.zst
.idea
.vscode
.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion brotli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ func testBrotliCompressSingleCase(s string) error {
if err != nil {
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
defer releaseBrotliReader(zr)
body, err := io.ReadAll(zr)
if err != nil {
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
if string(body) != s {
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
}
releaseBrotliReader(zr)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ func testGzipCompressSingleCase(s string) error {
if err != nil {
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
defer releaseGzipReader(zr)
body, err := io.ReadAll(zr)
if err != nil {
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
if string(body) != s {
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
}
releaseGzipReader(zr)
return nil
}

Expand Down
33 changes: 9 additions & 24 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import (
"sync"
"time"

"github.com/andybalholm/brotli"
"github.com/klauspost/compress/gzip"
"github.com/klauspost/compress/zstd"
"github.com/valyala/bytebufferpool"
)

Expand Down Expand Up @@ -1632,28 +1629,28 @@ func (h *fsHandler) newFSFile(f fs.File, fileInfo fs.FileInfo, compressed bool,

func readFileHeader(f io.Reader, compressed bool, fileEncoding string) ([]byte, error) {
r := f
var (
br *brotli.Reader
zr *gzip.Reader
zsr *zstd.Decoder
)
if compressed {
var err error
switch fileEncoding {
case "br":
if br, err = acquireBrotliReader(f); err != nil {
br, err := acquireBrotliReader(f)
if err != nil {
return nil, err
}
defer releaseBrotliReader(br)
r = br
case "gzip":
if zr, err = acquireGzipReader(f); err != nil {
zr, err := acquireGzipReader(f)
if err != nil {
return nil, err
}
defer releaseGzipReader(zr)
r = zr
case "zstd":
if zsr, err = acquireZstdReader(f); err != nil {
zsr, err := acquireZstdReader(f)
if err != nil {
return nil, err
}
defer releaseZstdReader(zsr)
r = zsr
}
}
Expand All @@ -1671,18 +1668,6 @@ func readFileHeader(f io.Reader, compressed bool, fileEncoding string) ([]byte,
return nil, err
}

if br != nil {
releaseBrotliReader(br)
}

if zr != nil {
releaseGzipReader(zr)
}

if zsr != nil {
releaseZstdReader(zsr)
}

return data, err
}

Expand Down
29 changes: 29 additions & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,35 @@ func TestServeFileCompressed(t *testing.T) {
if !bytes.Equal(body, expectedBody) {
t.Fatalf("unexpected body %q. expecting %q", body, expectedBody)
}

// request compressed zstd file
ctx.Request.Reset()
ctx.Request.SetRequestURI("http://foobar.com/baz")
ctx.Request.Header.Set(HeaderAcceptEncoding, "zstd")
ServeFile(&ctx, "fs.go")

s = ctx.Response.String()
br = bufio.NewReader(bytes.NewBufferString(s))
if err = resp.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}

ce = resp.Header.ContentEncoding()
if string(ce) != "zstd" {
t.Fatalf("Unexpected 'Content-Encoding' %q. Expecting %q", ce, "zstd")
}

body, err = resp.BodyUnzstd()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expectedBody, err = getFileContents("/fs.go")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !bytes.Equal(body, expectedBody) {
t.Fatalf("unexpected body %q. expecting %q", body, expectedBody)
}
}

func TestServeFileUncompressed(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion zstd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func testZstdCompressSingleCase(s string) error {
if err != nil {
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
defer releaseZstdReader(zr)
body, err := io.ReadAll(zr)
if err != nil {
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
if string(body) != s {
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
}
releaseZstdReader(zr)
return nil
}
Loading