Skip to content

Commit

Permalink
all: improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Nov 23, 2020
1 parent 060f923 commit 4dd382a
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions internal/aghio/limitedreadcloser.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (lrc *limitedReadCloser) Close() error {

// LimitReadCloser wraps ReadCloser to make it's Reader stop with
// ErrLimitReached after n bytes read.
func LimitReadCloser(rc io.ReadCloser, n int64) (new io.ReadCloser, err error) {
func LimitReadCloser(rc io.ReadCloser, n int64) (newRC io.ReadCloser, err error) {
if n < 0 {
return nil, fmt.Errorf("invalid n: %d", n)
return nil, fmt.Errorf("aghio: invalid n in LimitReadCloser: %d", n)
}
return &limitedReadCloser{
limit: n,
Expand Down
2 changes: 1 addition & 1 deletion internal/aghio/limitedreadcloser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestLimitReadCloser(t *testing.T) {
}, {
name: "negative",
n: -1,
want: fmt.Errorf("invalid n: -1"),
want: fmt.Errorf("aghio: invalid n in LimitReadCloser: -1"),
}}

for _, tc := range testCases {
Expand Down
3 changes: 2 additions & 1 deletion internal/home/auth_glinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ func glGetTokenDate(file string) uint32 {
log.Error("os.Open: %s", err)
return 0
}
defer f.Close()

fileReadCloser, err := aghio.LimitReadCloser(f, MaxFileSize)
if err != nil {
log.Error("LimitReadCloser: %s", err)
f.Close()
return 0
}
defer fileReadCloser.Close()
Expand Down
4 changes: 4 additions & 0 deletions internal/home/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"

"github.com/AdguardTeam/AdGuardHome/internal/aghio"

"github.com/AdguardTeam/golibs/log"
)

// middlerware is a wrapper function signature.
Expand All @@ -31,6 +33,8 @@ func limitRequestBody(h http.Handler) (limited http.Handler) {
var err error
r.Body, err = aghio.LimitReadCloser(r.Body, RequestBodySizeLimit)
if err != nil {
log.Error("limitRequestBody: %s", err)

return
}

Expand Down
3 changes: 2 additions & 1 deletion internal/home/whois.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ func (w *Whois) query(target, serverAddr string) (string, error) {
if err != nil {
return "", err
}
defer conn.Close()

connReadCloser, err := aghio.LimitReadCloser(conn, MaxConnReadSize)
if err != nil {
conn.Close()
return "", err
}
defer connReadCloser.Close()
Expand Down
3 changes: 2 additions & 1 deletion internal/update/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ func (u *Updater) GetVersionResponse(forceRecheck bool) (VersionInfo, error) {
if err != nil {
return VersionInfo{}, fmt.Errorf("updater: HTTP GET %s: %w", u.VersionURL, err)
}
defer resp.Body.Close()

resp.Body, err = aghio.LimitReadCloser(resp.Body, MaxResponseSize)
if err != nil {
resp.Body.Close()
return VersionInfo{}, fmt.Errorf("updater: LimitReadCloser: %w", err)
}
defer resp.Body.Close()
Expand Down
5 changes: 3 additions & 2 deletions internal/update/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ func (u *Updater) clean() {
}

// MaxPackageFileSize is a maximum package file length in bytes. The largest
// package whose size is limited by this constant has size of 9MiB for now.
// package whose size is limited by this constant currently has the size of
// approximately 9 MiB.
const MaxPackageFileSize = 32 * 1024 * 1024

// Download package file and save it to disk
Expand All @@ -228,10 +229,10 @@ func (u *Updater) downloadPackageFile(url string, filename string) error {
if err != nil {
return fmt.Errorf("http request failed: %w", err)
}
defer resp.Body.Close()

resp.Body, err = aghio.LimitReadCloser(resp.Body, MaxPackageFileSize)
if err != nil {
resp.Body.Close()
return fmt.Errorf("http request failed: %w", err)
}
defer resp.Body.Close()
Expand Down

0 comments on commit 4dd382a

Please sign in to comment.