From 70bf50955e080e952e25e232018c875bbe2d1369 Mon Sep 17 00:00:00 2001 From: Canelo Hill <172609632+canelohill@users.noreply.github.com> Date: Fri, 14 Jun 2024 10:51:13 -0700 Subject: [PATCH] Silence false positive lint warning in proxy code --- proxy.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/proxy.go b/proxy.go index e0f466b..18abf6e 100644 --- a/proxy.go +++ b/proxy.go @@ -6,6 +6,7 @@ package websocket import ( "bufio" + "bytes" "encoding/base64" "errors" "net" @@ -68,8 +69,18 @@ func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error) return nil, err } - if resp.StatusCode != 200 { - conn.Close() + // Close the response body to silence false positives from linters. Reset + // the buffered reader first to ensure that Close() does not read from + // conn. + // Note: Applications must call resp.Body.Close() on a response returned + // http.ReadResponse to inspect trailers or read another response from the + // buffered reader. The call to resp.Body.Close() does not release + // resources. + br.Reset(bytes.NewReader(nil)) + _ = resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + _ = conn.Close() f := strings.SplitN(resp.Status, " ", 2) return nil, errors.New(f[1]) }