From 7ae7d29741b02991a4d6f36c2fa23c611a1032a8 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sun, 12 Nov 2023 19:32:43 -0800 Subject: [PATCH] rest: resolve linter errors (#430) --- pkg/rest/apiv1_controller_test.go | 15 --------------- pkg/rest/client/apiv1_client_test.go | 5 +++-- pkg/rest/client/example_test.go | 4 ++-- pkg/rest/client/rest.go | 21 --------------------- pkg/rest/socketv1_controller.go | 26 +++++++++++++++++++------- pkg/rest/socketv2_controller.go | 25 ++++++++++++++++++------- 6 files changed, 42 insertions(+), 54 deletions(-) diff --git a/pkg/rest/apiv1_controller_test.go b/pkg/rest/apiv1_controller_test.go index e7a1fda..077bbd6 100644 --- a/pkg/rest/apiv1_controller_test.go +++ b/pkg/rest/apiv1_controller_test.go @@ -15,21 +15,6 @@ import ( "github.com/jhillyerd/enmime" ) -const ( - // JSON map keys - mailboxKey = "mailbox" - idKey = "id" - fromKey = "from" - toKey = "to" - subjectKey = "subject" - dateKey = "date" - sizeKey = "size" - headerKey = "header" - bodyKey = "body" - textKey = "text" - htmlKey = "html" -) - func TestRestMailboxList(t *testing.T) { // Setup mm := test.NewManager() diff --git a/pkg/rest/client/apiv1_client_test.go b/pkg/rest/client/apiv1_client_test.go index 1aa6667..22eb2f5 100644 --- a/pkg/rest/client/apiv1_client_test.go +++ b/pkg/rest/client/apiv1_client_test.go @@ -1,12 +1,13 @@ package client_test import ( - "github.com/gorilla/mux" "net/http" "net/http/httptest" "testing" "time" + "github.com/gorilla/mux" + "github.com/inbucket/inbucket/v3/pkg/rest/client" ) @@ -357,5 +358,5 @@ type jsonHandler struct { func (j *jsonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { j.called = true - w.Write([]byte(j.json)) + _, _ = w.Write([]byte(j.json)) } diff --git a/pkg/rest/client/example_test.go b/pkg/rest/client/example_test.go index f827162..592fce0 100644 --- a/pkg/rest/client/example_test.go +++ b/pkg/rest/client/example_test.go @@ -62,7 +62,7 @@ func exampleSetup() (baseURL string, teardown func()) { // Handle ListMailbox request. router.HandleFunc("/api/v1/mailbox/user1", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(`[ + _, _ = w.Write([]byte(`[ { "mailbox": "user1", "id": "20180107T224128-0000", @@ -79,7 +79,7 @@ func exampleSetup() (baseURL string, teardown func()) { // Handle GetMessage request. router.HandleFunc("/api/v1/mailbox/user1/20180107T224128-0000", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(`{ + _, _ = w.Write([]byte(`{ "mailbox": "user1", "id": "20180107T224128-0000", "from": "admin@inbucket.org", diff --git a/pkg/rest/client/rest.go b/pkg/rest/client/rest.go index 38a225d..d235ae0 100644 --- a/pkg/rest/client/rest.go +++ b/pkg/rest/client/rest.go @@ -58,24 +58,3 @@ func (c *restClient) doJSON(method string, uri string, v interface{}) error { return fmt.Errorf("%s for %q, unexpected %v: %s", method, uri, resp.StatusCode, resp.Status) } - -// doJSONBody performs an HTTP request with this client and marshalls the JSON response into v. -func (c *restClient) doJSONBody(method string, uri string, body []byte, v interface{}) error { - resp, err := c.do(method, uri, body) - if err != nil { - return err - } - - defer func() { - _ = resp.Body.Close() - }() - if resp.StatusCode == http.StatusOK { - if v == nil { - return nil - } - // Decode response body - return json.NewDecoder(resp.Body).Decode(v) - } - - return fmt.Errorf("%s for %q, unexpected %v: %s", method, uri, resp.StatusCode, resp.Status) -} diff --git a/pkg/rest/socketv1_controller.go b/pkg/rest/socketv1_controller.go index cfc82de..f1f2a01 100644 --- a/pkg/rest/socketv1_controller.go +++ b/pkg/rest/socketv1_controller.go @@ -72,12 +72,18 @@ func (ml *msgListenerV1) Delete(mailbox string, id string) error { func (ml *msgListenerV1) WSReader(conn *websocket.Conn) { slog := log.With().Str("module", "rest").Str("proto", "WebSocket"). Str("remote", conn.RemoteAddr().String()).Logger() + defer ml.Close() + conn.SetReadLimit(maxMessageSizeV1) - conn.SetReadDeadline(time.Now().Add(pongWaitV1)) + if err := conn.SetReadDeadline(time.Now().Add(pongWaitV1)); err != nil { + slog.Warn().Err(err).Msg("Failed to setup read deadline") + } conn.SetPongHandler(func(string) error { slog.Debug().Msg("Got pong") - conn.SetReadDeadline(time.Now().Add(pongWaitV1)) + if err := conn.SetReadDeadline(time.Now().Add(pongWaitV1)); err != nil { + slog.Warn().Err(err).Msg("Failed to set read deadline in pong") + } return nil }) @@ -101,6 +107,9 @@ func (ml *msgListenerV1) WSReader(conn *websocket.Conn) { // WSWriter makes sure the websocket client is still connected func (ml *msgListenerV1) WSWriter(conn *websocket.Conn) { + slog := log.With().Str("module", "rest").Str("proto", "WebSocket"). + Str("remote", conn.RemoteAddr().String()).Logger() + ticker := time.NewTicker(pingPeriodV1) defer func() { ticker.Stop() @@ -111,10 +120,12 @@ func (ml *msgListenerV1) WSWriter(conn *websocket.Conn) { for { select { case msg, ok := <-ml.c: - conn.SetWriteDeadline(time.Now().Add(writeWaitV1)) + if err := conn.SetWriteDeadline(time.Now().Add(writeWaitV1)); err != nil { + slog.Warn().Err(err).Msg("Failed to set write deadline for msg") + } if !ok { // msgListener closed, exit - conn.WriteMessage(websocket.CloseMessage, []byte{}) + _ = conn.WriteMessage(websocket.CloseMessage, []byte{}) return } if conn.WriteJSON(metadataToHeader(&msg)) != nil { @@ -123,13 +134,14 @@ func (ml *msgListenerV1) WSWriter(conn *websocket.Conn) { } case <-ticker.C: // Send ping - conn.SetWriteDeadline(time.Now().Add(writeWaitV1)) + if err := conn.SetWriteDeadline(time.Now().Add(writeWaitV1)); err != nil { + slog.Warn().Err(err).Msg("Failed to set write deadline for ping") + } if conn.WriteMessage(websocket.PingMessage, []byte{}) != nil { // Write error return } - log.Debug().Str("module", "rest").Str("proto", "WebSocket"). - Str("remote", conn.RemoteAddr().String()).Msg("Sent ping") + slog.Debug().Msg("Sent ping") } } } diff --git a/pkg/rest/socketv2_controller.go b/pkg/rest/socketv2_controller.go index 1f41b98..ec4657a 100644 --- a/pkg/rest/socketv2_controller.go +++ b/pkg/rest/socketv2_controller.go @@ -91,11 +91,16 @@ func (ml *msgListenerV2) WSReader(conn *websocket.Conn) { slog := log.With().Str("module", "rest").Str("proto", "WebSocket"). Str("remote", conn.RemoteAddr().String()).Logger() defer ml.Close() + conn.SetReadLimit(maxMessageSizeV2) - conn.SetReadDeadline(time.Now().Add(pongWaitV2)) + if err := conn.SetReadDeadline(time.Now().Add(pongWaitV2)); err != nil { + slog.Warn().Err(err).Msg("Failed to setup read deadline") + } conn.SetPongHandler(func(string) error { slog.Debug().Msg("Got pong") - conn.SetReadDeadline(time.Now().Add(pongWaitV2)) + if err := conn.SetReadDeadline(time.Now().Add(pongWaitV2)); err != nil { + slog.Warn().Err(err).Msg("Failed to set read deadline in pong") + } return nil }) @@ -119,6 +124,9 @@ func (ml *msgListenerV2) WSReader(conn *websocket.Conn) { // WSWriter makes sure the websocket client is still connected func (ml *msgListenerV2) WSWriter(conn *websocket.Conn) { + slog := log.With().Str("module", "rest").Str("proto", "WebSocket"). + Str("remote", conn.RemoteAddr().String()).Logger() + ticker := time.NewTicker(pingPeriodV2) defer func() { ticker.Stop() @@ -129,10 +137,12 @@ func (ml *msgListenerV2) WSWriter(conn *websocket.Conn) { for { select { case event, ok := <-ml.c: - conn.SetWriteDeadline(time.Now().Add(writeWaitV2)) + if err := conn.SetWriteDeadline(time.Now().Add(writeWaitV2)); err != nil { + slog.Warn().Err(err).Msg("Failed to set write deadline for msg") + } if !ok { // msgListener closed, exit - conn.WriteMessage(websocket.CloseMessage, []byte{}) + _ = conn.WriteMessage(websocket.CloseMessage, []byte{}) return } if conn.WriteJSON(event) != nil { @@ -141,13 +151,14 @@ func (ml *msgListenerV2) WSWriter(conn *websocket.Conn) { } case <-ticker.C: // Send ping - conn.SetWriteDeadline(time.Now().Add(writeWaitV2)) + if err := conn.SetWriteDeadline(time.Now().Add(writeWaitV2)); err != nil { + slog.Warn().Err(err).Msg("Failed to set write deadline for ping") + } if conn.WriteMessage(websocket.PingMessage, []byte{}) != nil { // Write error return } - log.Debug().Str("module", "rest").Str("proto", "WebSocket"). - Str("remote", conn.RemoteAddr().String()).Msg("Sent ping") + slog.Debug().Msg("Sent ping") } } }