launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/utils/http.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package utils
     5  
     6  import (
     7  	"crypto/tls"
     8  	"net/http"
     9  	"sync"
    10  )
    11  
    12  var insecureClient = (*http.Client)(nil)
    13  var insecureClientMutex = sync.Mutex{}
    14  
    15  func GetNonValidatingHTTPClient() *http.Client {
    16  	insecureClientMutex.Lock()
    17  	defer insecureClientMutex.Unlock()
    18  	if insecureClient == nil {
    19  		insecureConfig := &tls.Config{InsecureSkipVerify: true}
    20  		insecureTransport := NewHttpTLSTransport(insecureConfig)
    21  		insecureClient = &http.Client{Transport: insecureTransport}
    22  	}
    23  	return insecureClient
    24  }
    25  
    26  // NewHttpTLSTransport returns a new http.Transport constructed with the TLS config
    27  // and the necessary parameters for Juju.
    28  func NewHttpTLSTransport(tlsConfig *tls.Config) *http.Transport {
    29  	// See https://code.google.com/p/go/issues/detail?id=4677
    30  	// We need to force the connection to close each time so that we don't
    31  	// hit the above Go bug.
    32  	return &http.Transport{
    33  		TLSClientConfig:   tlsConfig,
    34  		DisableKeepAlives: true,
    35  	}
    36  }
    37  
    38  // NewHttpTransport returns a new http.Transport constructed with the necessary
    39  // parameters for Juju.
    40  func NewHttpTransport() *http.Transport {
    41  	// See https://code.google.com/p/go/issues/detail?id=4677
    42  	// We need to force the connection to close each time so that we don't
    43  	// hit the above Go bug.
    44  	return &http.Transport{
    45  		Proxy:             http.ProxyFromEnvironment,
    46  		DisableKeepAlives: true,
    47  	}
    48  }