github.com/minio/console@v1.3.0/pkg/http/http.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package http
    18  
    19  import (
    20  	"io"
    21  	"net/http"
    22  )
    23  
    24  // ClientI interface with all functions to be implemented
    25  // by mock when testing, it should include all HttpClient respective api calls
    26  // that are used within this project.
    27  type ClientI interface {
    28  	Get(url string) (resp *http.Response, err error)
    29  	Post(url, contentType string, body io.Reader) (resp *http.Response, err error)
    30  	Do(req *http.Request) (*http.Response, error)
    31  }
    32  
    33  // Client is an HTTP Interface implementation
    34  //
    35  // Define the structure of a http client and define the functions that are actually used
    36  type Client struct {
    37  	Client *http.Client
    38  }
    39  
    40  // Get implements http.Client.Get()
    41  func (c *Client) Get(url string) (resp *http.Response, err error) {
    42  	return c.Client.Get(url)
    43  }
    44  
    45  // Post implements http.Client.Post()
    46  func (c *Client) Post(url, contentType string, body io.Reader) (resp *http.Response, err error) {
    47  	return c.Client.Post(url, contentType, body)
    48  }
    49  
    50  // Do implement http.Client.Do()
    51  func (c *Client) Do(req *http.Request) (*http.Response, error) {
    52  	return c.Client.Do(req)
    53  }
    54  
    55  // DrainBody close non nil response with any response Body.
    56  // convenient wrapper to drain any remaining data on response body.
    57  //
    58  // Subsequently this allows golang http RoundTripper
    59  // to re-use the same connection for future requests.
    60  func DrainBody(respBody io.ReadCloser) {
    61  	// Callers should close resp.Body when done reading from it.
    62  	// If resp.Body is not closed, the Client's underlying RoundTripper
    63  	// (typically Transport) may not be able to re-use a persistent TCP
    64  	// connection to the server for a subsequent "keep-alive" request.
    65  	if respBody != nil {
    66  		// Drain any remaining Body and then close the connection.
    67  		// Without this closing connection would disallow re-using
    68  		// the same connection for future uses.
    69  		//  - http://stackoverflow.com/a/17961593/4465767
    70  		defer respBody.Close()
    71  		io.Copy(io.Discard, respBody)
    72  	}
    73  }