Skip to content

Commit

Permalink
Change empty string checks to be more idiomatic (valyala#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-Cheng committed Feb 12, 2024
1 parent 2e8d48b commit 89f0aff
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ linters-settings:
"all",
"-ST1000", # at least one file in a package should have a package comment
]
gocritic:
enabled-checks:
- emptyStringTest

issues:
# Show all issues from a linter.
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ func newClientTLSConfig(c *tls.Config, addr string) *tls.Config {
c = c.Clone()
}

if len(c.ServerName) == 0 {
if c.ServerName == "" {
serverName := tlsServerName(addr)
if serverName == "*" {
c.InsecureSkipVerify = true
Expand Down
2 changes: 1 addition & 1 deletion expvarhandler/expvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ExpvarHandler(ctx *fasthttp.RequestCtx) {

func getExpvarRegexp(ctx *fasthttp.RequestCtx) (*regexp.Regexp, error) {
r := string(ctx.QueryArgs().Peek("r"))
if len(r) == 0 {
if r == "" {
return defaultRE, nil
}
rr, err := regexp.Compile(r)
Expand Down
47 changes: 43 additions & 4 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sync"
"time"

"github.com/klauspost/compress/zstd"
"github.com/andybalholm/brotli"
"github.com/klauspost/compress/gzip"
"github.com/valyala/bytebufferpool"
Expand Down Expand Up @@ -370,6 +371,7 @@ const FSCompressedFileSuffix = ".fasthttp.gz"
var FSCompressedFileSuffixes = map[string]string{
"gzip": ".fasthttp.gz",
"br": ".fasthttp.br",
"zstd": ".fasthttp.zst",
}

// FSHandlerCacheDuration is the default expiration duration for inactive
Expand Down Expand Up @@ -459,8 +461,10 @@ func (fs *FS) initRequestHandler() {
}

compressedFileSuffixes := fs.CompressedFileSuffixes
if len(compressedFileSuffixes["br"]) == 0 || len(compressedFileSuffixes["gzip"]) == 0 ||
compressedFileSuffixes["br"] == compressedFileSuffixes["gzip"] {
if compressedFileSuffixes["br"] == "" || compressedFileSuffixes["gzip"] == "" ||
len(compressedFileSuffixes["zstd"]) == 0 || compressedFileSuffixes["br"] == compressedFileSuffixes["gzip"] ||
compressedFileSuffixes["br"] == compressedFileSuffixes["zstd"] ||
compressedFileSuffixes["gzip"] == compressedFileSuffixes["zstd"] {
// Copy global map
compressedFileSuffixes = make(map[string]string, len(FSCompressedFileSuffixes))
for k, v := range FSCompressedFileSuffixes {
Expand All @@ -471,6 +475,7 @@ func (fs *FS) initRequestHandler() {
if len(fs.CompressedFileSuffix) > 0 {
compressedFileSuffixes["gzip"] = fs.CompressedFileSuffix
compressedFileSuffixes["br"] = FSCompressedFileSuffixes["br"]
compressedFileSuffixes["zstd"] = FSCompressedFileSuffixes["zstd"]
}

h := &fsHandler{
Expand Down Expand Up @@ -794,6 +799,7 @@ const (
defaultCacheKind CacheKind = iota
brotliCacheKind
gzipCacheKind
zstdCacheKind
)

func newCacheManager(fs *FS) cacheManager {
Expand Down Expand Up @@ -1040,6 +1046,11 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
mustCompress = true
fileCacheKind = gzipCacheKind
fileEncoding = "gzip"
} else if ctx.Request.Header.HasAcceptEncodingBytes(strZstd) {
mustCompress = true
fileCacheKind = zstdCacheKind
fileEncoding = "zstd"

}
}

Expand Down Expand Up @@ -1101,6 +1112,8 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
hdr.SetContentEncodingBytes(strBr)
} else if fileEncoding == "gzip" {
hdr.SetContentEncodingBytes(strGzip)
} else if fileEncoding == "zstd" {
hdr.SetContentEncodingBytes(strZstd)
}
}

Expand Down Expand Up @@ -1308,6 +1321,8 @@ nestedContinue:
zbuf.B = AppendBrotliBytesLevel(zbuf.B, w.B, CompressDefaultCompression)
} else if fileEncoding == "gzip" {
zbuf.B = AppendGzipBytesLevel(zbuf.B, w.B, CompressDefaultCompression)
} else if fileEncoding == "zstd" {
zbuf.B = AppendZstdBytesLevel(zbuf.B, w.B, CompressZstdDefault)
}
w = &zbuf
}
Expand Down Expand Up @@ -1420,6 +1435,13 @@ func (h *fsHandler) compressFileNolock(
err = err1
}
releaseStacklessGzipWriter(zw, CompressDefaultCompression)
} else if fileEncoding == "zstd" {
zw := acquireStacklessZstdWriter(zf, CompressZstdDefault)
_, err = copyZeroAlloc(zw, f)
if err1 := zw.Flush(); err == nil {
err = err1
}
releaseStacklessZstdWriter(zw, CompressZstdDefault)
}
_ = zf.Close()
_ = f.Close()
Expand Down Expand Up @@ -1457,6 +1479,13 @@ func (h *fsHandler) newCompressedFSFileCache(f fs.File, fileInfo fs.FileInfo, fi
err = err1
}
releaseStacklessGzipWriter(zw, CompressDefaultCompression)
} else if fileEncoding == "zstd" {
zw := acquireStacklessZstdWriter(w, CompressZstdDefault)
_, err = copyZeroAlloc(zw, f)
if err1 := zw.Flush(); err == nil {
err = err1
}
releaseStacklessZstdWriter(zw, CompressZstdDefault)
}
defer func() { _ = f.Close() }()

Expand Down Expand Up @@ -1600,8 +1629,9 @@ 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
br *brotli.Reader
zr *gzip.Reader
zsr *zstd.Decoder
)
if compressed {
var err error
Expand All @@ -1615,6 +1645,11 @@ func readFileHeader(f io.Reader, compressed bool, fileEncoding string) ([]byte,
return nil, err
}
r = zr
} else if fileEncoding == "zstd" {
if zsr, err = acquireZstdReader(f); err != nil {
return nil, err
}
r = zsr
}
}

Expand All @@ -1639,6 +1674,10 @@ func readFileHeader(f io.Reader, compressed bool, fileEncoding string) ([]byte,
releaseGzipReader(zr)
}

if zsr != nil {
releaseZstdReader(zsr)
}

return data, err
}

Expand Down
2 changes: 1 addition & 1 deletion header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,7 @@ type bufioPeekReader struct {
}

func (r *bufioPeekReader) Read(b []byte) (int, error) {
if len(r.s) == 0 {
if r.s == "" {
return 0, io.EOF
}

Expand Down
4 changes: 2 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ func (req *Request) MultipartForm() (*multipart.Form, error) {
}

req.multipartFormBoundary = string(req.Header.MultipartFormBoundary())
if len(req.multipartFormBoundary) == 0 {
if req.multipartFormBoundary == "" {
return nil, ErrNoMultipartForm
}

Expand Down Expand Up @@ -1014,7 +1014,7 @@ func marshalMultipartForm(f *multipart.Form, boundary string) ([]byte, error) {
func WriteMultipartForm(w io.Writer, f *multipart.Form, boundary string) error {
// Do not care about memory allocations here, since multipart
// form processing is slow.
if len(boundary) == 0 {
if boundary == "" {
return errors.New("form boundary cannot be empty")
}

Expand Down
4 changes: 2 additions & 2 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2214,7 +2214,7 @@ func testRequestSuccess(t *testing.T, method, requestURI, host, userAgent, body,
if string(req1.Header.Method()) != expectedMethod {
t.Fatalf("Unexpected method: %q. Expected %q", req1.Header.Method(), expectedMethod)
}
if len(requestURI) == 0 {
if requestURI == "" {
requestURI = "/"
}
if string(req1.Header.RequestURI()) != requestURI {
Expand Down Expand Up @@ -2467,7 +2467,7 @@ func testRequestPostArgsError(t *testing.T, req *Request, s string) {
t.Fatalf("Unexpected error when reading %q: %v", s, err)
}
ss := req.PostArgs().String()
if len(ss) != 0 {
if ss != "" {
t.Fatalf("unexpected post args: %q. Expecting empty post args", ss)
}
}
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,7 @@ func (s *Server) ServeTLSEmbed(ln net.Listener, certData, keyData []byte) error
// This function allows programmer to handle multiple domains
// in one server structure. See examples/multidomain.
func (s *Server) AppendCert(certFile, keyFile string) error {
if len(certFile) == 0 && len(keyFile) == 0 {
if certFile == "" && keyFile == "" {
return errNoCertOrKeyProvided
}

Expand Down

0 comments on commit 89f0aff

Please sign in to comment.