http.go 1017 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package transport // import "github.com/docker/docker/pkg/plugins/transport"
  2. import (
  3. "io"
  4. "net/http"
  5. "strings"
  6. )
  7. // HTTPTransport holds an [http.RoundTripper]
  8. // and information about the scheme and address the transport
  9. // sends request to.
  10. type HTTPTransport struct {
  11. http.RoundTripper
  12. scheme string
  13. addr string
  14. }
  15. // NewHTTPTransport creates a new HTTPTransport.
  16. func NewHTTPTransport(r http.RoundTripper, scheme, addr string) *HTTPTransport {
  17. return &HTTPTransport{
  18. RoundTripper: r,
  19. scheme: scheme,
  20. addr: addr,
  21. }
  22. }
  23. // NewRequest creates a new http.Request and sets the URL
  24. // scheme and address with the transport's fields.
  25. func (t HTTPTransport) NewRequest(path string, data io.Reader) (*http.Request, error) {
  26. if !strings.HasPrefix(path, "/") {
  27. path = "/" + path
  28. }
  29. req, err := http.NewRequest(http.MethodPost, path, data)
  30. if err != nil {
  31. return nil, err
  32. }
  33. req.Header.Add("Accept", VersionMimetype)
  34. req.URL.Scheme = t.scheme
  35. req.URL.Host = t.addr
  36. return req, nil
  37. }