Browse Source

Fix: close response body (#484)

Loyalsoldier 5 years ago
parent
commit
5e99737c12
3 changed files with 5 additions and 3 deletions
  1. 2 1
      proxy/http/client.go
  2. 2 1
      proxy/http/server.go
  3. 1 1
      testing/scenarios/http_test.go

+ 2 - 1
proxy/http/client.go

@@ -164,11 +164,12 @@ func setUpHTTPTunnel(ctx context.Context, dest net.Destination, target string, u
 			return nil, err
 			return nil, err
 		}
 		}
 
 
-		resp, err := http.ReadResponse(bufio.NewReader(rawConn), req) // nolint: bodyclose
+		resp, err := http.ReadResponse(bufio.NewReader(rawConn), req)
 		if err != nil {
 		if err != nil {
 			rawConn.Close()
 			rawConn.Close()
 			return nil, err
 			return nil, err
 		}
 		}
+		defer resp.Body.Close()
 
 
 		if resp.StatusCode != http.StatusOK {
 		if resp.StatusCode != http.StatusOK {
 			rawConn.Close()
 			rawConn.Close()

+ 2 - 1
proxy/http/server.go

@@ -279,7 +279,7 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, wri
 
 
 	responseDone := func() error {
 	responseDone := func() error {
 		responseReader := bufio.NewReaderSize(&buf.BufferedReader{Reader: link.Reader}, buf.Size)
 		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 {
 		if err == nil {
 			http_proto.RemoveHopByHopHeaders(response.Header)
 			http_proto.RemoveHopByHopHeaders(response.Header)
 			if response.ContentLength >= 0 {
 			if response.ContentLength >= 0 {
@@ -291,6 +291,7 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, wri
 				response.Close = true
 				response.Close = true
 				result = nil
 				result = nil
 			}
 			}
+			defer response.Body.Close()
 		} else {
 		} else {
 			newError("failed to read response from ", request.Host).Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx))
 			newError("failed to read response from ", request.Host).Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx))
 			response = &http.Response{
 			response = &http.Response{

+ 1 - 1
testing/scenarios/http_test.go

@@ -129,7 +129,7 @@ func TestHttpError(t *testing.T) {
 			Transport: transport,
 			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)
 		common.Must(err)
 		defer resp.Body.Close()
 		defer resp.Body.Close()
 		if resp.StatusCode != 503 {
 		if resp.StatusCode != 503 {