From 5e99737c12cf63da4bc41b2d6dde4eb292cc8937 Mon Sep 17 00:00:00 2001 From: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> Date: Thu, 3 Dec 2020 16:07:41 +0800 Subject: [PATCH] Fix: close response body (#484) --- proxy/http/client.go | 3 ++- proxy/http/server.go | 3 ++- testing/scenarios/http_test.go | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/proxy/http/client.go b/proxy/http/client.go index 51a2b6de4..c3481633a 100644 --- a/proxy/http/client.go +++ b/proxy/http/client.go @@ -164,11 +164,12 @@ func setUpHTTPTunnel(ctx context.Context, dest net.Destination, target string, u return nil, err } - resp, err := http.ReadResponse(bufio.NewReader(rawConn), req) // nolint: bodyclose + resp, err := http.ReadResponse(bufio.NewReader(rawConn), req) if err != nil { rawConn.Close() return nil, err } + defer resp.Body.Close() if resp.StatusCode != http.StatusOK { rawConn.Close() diff --git a/proxy/http/server.go b/proxy/http/server.go index 5cf5c5ed4..7bc9de6b3 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -279,7 +279,7 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, wri responseDone := func() error { responseReader := bufio.NewReaderSize(&buf.BufferedReader{Reader: link.Reader}, buf.Size) - response, err := http.ReadResponse(responseReader, request) // nolint: bodyclose + response, err := http.ReadResponse(responseReader, request) if err == nil { http_proto.RemoveHopByHopHeaders(response.Header) if response.ContentLength >= 0 { @@ -291,6 +291,7 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, wri response.Close = true result = nil } + defer response.Body.Close() } else { newError("failed to read response from ", request.Host).Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx)) response = &http.Response{ diff --git a/testing/scenarios/http_test.go b/testing/scenarios/http_test.go index 17b21a527..06b9495a0 100644 --- a/testing/scenarios/http_test.go +++ b/testing/scenarios/http_test.go @@ -129,7 +129,7 @@ func TestHttpError(t *testing.T) { Transport: transport, } - resp, err := client.Get("http://127.0.0.1:" + dest.Port.String()) // nolint: bodyclose + resp, err := client.Get("http://127.0.0.1:" + dest.Port.String()) common.Must(err) defer resp.Body.Close() if resp.StatusCode != 503 {