Skip to content

Commit

Permalink
fix lint warnings (issue 2541) (#2551)
Browse files Browse the repository at this point in the history
* Lint: fix some errcheck #2541

* Lint: fix passing structcheck #2541

* Lint: update fix structcheck #2541

* Lint: fix errcheck for basicauth, browse, fastcgi_test #2541

* Lint: fix errcheck for browse, fastcgi_test, fcgiclient, fcgiclient_test #2541

* Lint: fix errcheck for responsefilter_test, fcgilient_test #2541

* Lint: fix errcheck for header_test #2541

* Lint: update errcheck for fcgiclient_test #2541

* Lint: fix errcheck for server, header_test, fastcgi_test, https_test, recorder_test #2541

* Lint: fix errcheck for tplcontext, vhosttrie_test, internal_test, handler_test #2541

* Lint: fix errcheck for log_test, markdown mholt#2541

* Lint: fix errcheck for policy, body_test, proxy_test #2541

* Lint: fix errcheck for on multiple packages #2541

- reverseproxy
- reverseproxy_test
- upstream
- upstream_test
- body_test

* Lint: fix errcheck in multiple packages mholt#2541
- handler_test
- redirect_test
- requestid_test
- rewrite_test
- fileserver_test

* Lint: fix errcheck in multiple packages mholt#2541

- websocket
- setup
- collection
- redirect_test
- templates_test

* Lint: fix errcheck in logger test #2541

run goimports against #2551
- lexer_test
- log_test
- markdown

* Update caddyhttp/httpserver/logger_test.go

Co-Authored-By: Inconnu08 <taufiqrx8@gmail.com>

* Update log_test.go

* Lint: fix scope in logger_test #2541

* remove redundant err check in logger_test #2541

* fix alias in logger_test #2541

* fix import for format #2541

* refactor variable names and error check #2541
  • Loading branch information
Inconnu08 authored and mholt committed Apr 22, 2019
1 parent 0c3d90e commit c32a0f5
Show file tree
Hide file tree
Showing 35 changed files with 498 additions and 220 deletions.
12 changes: 10 additions & 2 deletions caddy_test.go
Expand Up @@ -16,6 +16,7 @@ package caddy

import (
"fmt"
"log"
"reflect"
"sync"
"testing"
Expand Down Expand Up @@ -103,13 +104,20 @@ func TestCaddyRestartCallbacks(t *testing.T) {
return nil
})

c.instance.Restart(CaddyfileInput{Contents: []byte(""), ServerTypeName: serverName})
_, err := c.instance.Restart(CaddyfileInput{Contents: []byte(""), ServerTypeName: serverName})
if err != nil {
log.Printf("[ERROR] Restart failed: %v", err)
}

if !reflect.DeepEqual(calls, test.expectedCalls) {
t.Errorf("Test %d: Callbacks expected: %v, got: %v", i, test.expectedCalls, calls)
}

c.instance.Stop()
err = c.instance.Stop()
if err != nil {
log.Printf("[ERROR] Stop failed: %v", err)
}

c.instance.Wait()
}

Expand Down
5 changes: 4 additions & 1 deletion caddyfile/lexer_test.go
Expand Up @@ -15,6 +15,7 @@
package caddyfile

import (
"log"
"strings"
"testing"
)
Expand Down Expand Up @@ -158,7 +159,9 @@ func TestLexer(t *testing.T) {

func tokenize(input string) (tokens []Token) {
l := lexer{}
l.load(strings.NewReader(input))
if err := l.load(strings.NewReader(input)); err != nil {
log.Printf("[ERROR] load failed: %v", err)
}
for l.next() {
tokens = append(tokens, l.token)
}
Expand Down
9 changes: 7 additions & 2 deletions caddyhttp/basicauth/basicauth.go
Expand Up @@ -26,6 +26,7 @@ import (
"crypto/subtle"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -193,11 +194,15 @@ func PlainMatcher(passw string) PasswordMatcher {
// compare hashes of equal length instead of actual password
// to avoid leaking password length
passwHash := sha1.New()
passwHash.Write([]byte(passw))
if _, err := passwHash.Write([]byte(passw)); err != nil {
log.Printf("[ERROR] unable to write password hash: %v", err)
}
passwSum := passwHash.Sum(nil)
return func(pw string) bool {
pwHash := sha1.New()
pwHash.Write([]byte(pw))
if _, err := pwHash.Write([]byte(pw)); err != nil {
log.Printf("[ERROR] unable to write password hash: %v", err)
}
pwSum := pwHash.Sum(nil)
return subtle.ConstantTimeCompare([]byte(pwSum), []byte(passwSum)) == 1
}
Expand Down
32 changes: 16 additions & 16 deletions caddyhttp/browse/browse.go
Expand Up @@ -59,34 +59,34 @@ type Config struct {

// A Listing is the context used to fill out a template.
type Listing struct {
// The name of the directory (the last element of the path)
// The name of the directory (the last element of the path).
Name string

// The full path of the request
// The full path of the request.
Path string

// Whether the parent directory is browsable
// Whether the parent directory is browse-able.
CanGoUp bool

// The items (files and folders) in the path
// The items (files and folders) in the path.
Items []FileInfo

// The number of directories in the listing
// The number of directories in the listing.
NumDirs int

// The number of files (items that aren't directories) in the listing
// The number of files (items that aren't directories) in the listing.
NumFiles int

// Which sorting order is used
// Which sorting order is used.
Sort string

// And which order
// And which order.
Order string

// If ≠0 then Items have been limited to that many elements
// If ≠0 then Items have been limited to that many elements.
ItemsLimitedTo int

// Optional custom variables for use in browse templates
// Optional custom variables for use in browse templates.
User interface{}

httpserver.Context
Expand Down Expand Up @@ -244,7 +244,7 @@ func (l Listing) applySort() {

func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config *Config) (Listing, bool) {
var (
fileinfos []FileInfo
fileInfos []FileInfo
dirCount, fileCount int
hasIndexFile bool
)
Expand Down Expand Up @@ -272,14 +272,14 @@ func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config
continue
}

url := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name

fileinfos = append(fileinfos, FileInfo{
fileInfos = append(fileInfos, FileInfo{
IsDir: isDir,
IsSymlink: isSymlink(f),
Name: f.Name(),
Size: f.Size(),
URL: url.String(),
URL: u.String(),
ModTime: f.ModTime().UTC(),
Mode: f.Mode(),
})
Expand All @@ -289,7 +289,7 @@ func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config
Name: path.Base(urlPath),
Path: urlPath,
CanGoUp: canGoUp,
Items: fileinfos,
Items: fileInfos,
NumDirs: dirCount,
NumFiles: fileCount,
}, hasIndexFile
Expand Down Expand Up @@ -504,7 +504,7 @@ func (b Browse) ServeListing(w http.ResponseWriter, r *http.Request, requestedFi

}

buf.WriteTo(w)
_, _ = buf.WriteTo(w)

return http.StatusOK, nil
}
Expand Down
54 changes: 37 additions & 17 deletions caddyhttp/fastcgi/fastcgi_test.go
Expand Up @@ -16,6 +16,7 @@ package fastcgi

import (
"context"
"log"
"net"
"net/http"
"net/http/fcgi"
Expand All @@ -39,11 +40,20 @@ func TestServeHTTP(t *testing.T) {
if err != nil {
t.Fatalf("Unable to create listener for test: %v", err)
}
defer listener.Close()
go fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", bodyLenStr)
w.Write([]byte(body))
}))
defer func() { _ = listener.Close() }()

go func() {
err := fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", bodyLenStr)
_, err := w.Write([]byte(body))
if err != nil {
log.Printf("[ERROR] unable to write header: %v", err)
}
}))
if err != nil {
log.Printf("[ERROR] unable to start server: %v", err)
}
}()

handler := Handler{
Next: nil,
Expand Down Expand Up @@ -147,15 +157,15 @@ func TestBuildEnv(t *testing.T) {
SplitPath: ".php",
IndexFiles: []string{"index.php"},
}
url, err := url.Parse("http://localhost:2015/fgci_test.php?test=foobar")
u, err := url.Parse("http://localhost:2015/fgci_test.php?test=foobar")
if err != nil {
t.Error("Unexpected error:", err.Error())
}

var newReq = func() *http.Request {
r := http.Request{
Method: "GET",
URL: url,
URL: u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Expand Down Expand Up @@ -274,7 +284,7 @@ func TestReadTimeout(t *testing.T) {
if err != nil {
t.Fatalf("Test %d: Unable to create listener for test: %v", i, err)
}
defer listener.Close()
defer func() { _ = listener.Close() }()

handler := Handler{
Next: nil,
Expand All @@ -293,11 +303,16 @@ func TestReadTimeout(t *testing.T) {
w := httptest.NewRecorder()

wg.Add(1)
go fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(test.sleep)
w.WriteHeader(http.StatusOK)
wg.Done()
}))
go func() {
err := fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(test.sleep)
w.WriteHeader(http.StatusOK)
wg.Done()
}))
if err != nil {
log.Printf("[ERROR] unable to start server: %v", err)
}
}()

got, err := handler.ServeHTTP(w, r)
if test.shouldErr {
Expand Down Expand Up @@ -334,7 +349,7 @@ func TestSendTimeout(t *testing.T) {
if err != nil {
t.Fatalf("Test %d: Unable to create listener for test: %v", i, err)
}
defer listener.Close()
defer func() { _ = listener.Close() }()

handler := Handler{
Next: nil,
Expand All @@ -352,9 +367,14 @@ func TestSendTimeout(t *testing.T) {
}
w := httptest.NewRecorder()

go fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
go func() {
err := fcgi.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
if err != nil {
log.Printf("[ERROR] unable to start server: %v", err)
}
}()

got, err := handler.ServeHTTP(w, r)
if test.shouldErr {
Expand Down
18 changes: 8 additions & 10 deletions caddyhttp/fastcgi/fcgiclient.go
Expand Up @@ -175,15 +175,13 @@ func (rec *record) read(r io.Reader) (buf []byte, err error) {
// FCGIClient implements a FastCGI client, which is a standard for
// interfacing external applications with Web servers.
type FCGIClient struct {
mutex sync.Mutex
rwc io.ReadWriteCloser
h header
buf bytes.Buffer
stderr bytes.Buffer
keepAlive bool
reqID uint16
readTimeout time.Duration
sendTimeout time.Duration
mutex sync.Mutex
rwc io.ReadWriteCloser
h header
buf bytes.Buffer
stderr bytes.Buffer
keepAlive bool
reqID uint16
}

// DialWithDialerContext connects to the fcgi responder at the specified network address, using custom net.Dialer
Expand Down Expand Up @@ -397,7 +395,7 @@ func (c *FCGIClient) Do(p map[string]string, req io.Reader) (r io.Reader, err er

body := newWriter(c, Stdin)
if req != nil {
io.Copy(body, req)
_, _ = io.Copy(body, req)
}
body.Close()

Expand Down

1 comment on commit c32a0f5

@jungle-boogie
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something here affects #2694

Please sign in to comment.