transport.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Package transport provides function to send request to remote endpoints.
  2. package transport
  3. import (
  4. "fmt"
  5. "net/http"
  6. "github.com/docker/go-connections/sockets"
  7. )
  8. // apiTransport holds information about the http transport to connect with the API.
  9. type apiTransport struct {
  10. *http.Client
  11. *tlsInfo
  12. transport *http.Transport
  13. }
  14. // NewTransportWithHTTP creates a new transport based on the provided proto, address and http client.
  15. // It uses Docker's default http transport configuration if the client is nil.
  16. // It does not modify the client's transport if it's not nil.
  17. func NewTransportWithHTTP(proto, addr string, client *http.Client) (Client, error) {
  18. var transport *http.Transport
  19. if client != nil {
  20. tr, ok := client.Transport.(*http.Transport)
  21. if !ok {
  22. return nil, fmt.Errorf("unable to verify TLS configuration, invalid transport %v", client.Transport)
  23. }
  24. transport = tr
  25. } else {
  26. transport = defaultTransport(proto, addr)
  27. client = &http.Client{
  28. Transport: transport,
  29. }
  30. }
  31. return &apiTransport{
  32. Client: client,
  33. tlsInfo: &tlsInfo{transport.TLSClientConfig},
  34. transport: transport,
  35. }, nil
  36. }
  37. // CancelRequest stops a request execution.
  38. func (a *apiTransport) CancelRequest(req *http.Request) {
  39. a.transport.CancelRequest(req)
  40. }
  41. // defaultTransport creates a new http.Transport with Docker's
  42. // default transport configuration.
  43. func defaultTransport(proto, addr string) *http.Transport {
  44. tr := new(http.Transport)
  45. sockets.ConfigureTransport(tr, proto, addr)
  46. return tr
  47. }
  48. var _ Client = &apiTransport{}