amuz.es/src/infra/goutils@v0.1.3/http/client/round_tripper.go (about)

     1  package client
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  const (
     8  	connectionHeaderKey          = "Connection"
     9  	connectionUserAgentHeaderKey = "User-Agent"
    10  	connectionCloseHeader        = "close"
    11  	connectionKeepAliveHeader    = "keep-alive"
    12  )
    13  
    14  var (
    15  	connectionCloseHeaderValue     = []string{connectionCloseHeader}
    16  	connectionKeepAliveHeaderValue = []string{connectionKeepAliveHeader}
    17  )
    18  
    19  type predefinedHeaderTransport struct {
    20  	useragentName []string
    21  	http.Transport
    22  }
    23  
    24  func (pht *predefinedHeaderTransport) RoundTrip(req *http.Request) (res *http.Response, err error) {
    25  	var connectionValue []string
    26  	if pht.DisableKeepAlives {
    27  		connectionValue = connectionCloseHeaderValue
    28  	} else {
    29  		connectionValue = connectionKeepAliveHeaderValue
    30  	}
    31  	req.Header[connectionHeaderKey] = connectionValue
    32  	req.Header[connectionUserAgentHeaderKey] = pht.useragentName
    33  	res, err = pht.Transport.RoundTrip(req)
    34  	return
    35  }