http.go 819 B

123456789101112131415161718192021222324252627282930313233343536
  1. package transport
  2. import (
  3. "io"
  4. "net/http"
  5. )
  6. // httpTransport holds an http.RoundTripper
  7. // and information about the scheme and address the transport
  8. // sends request to.
  9. type httpTransport struct {
  10. http.RoundTripper
  11. scheme string
  12. addr string
  13. }
  14. // NewHTTPTransport creates a new httpTransport.
  15. func NewHTTPTransport(r http.RoundTripper, scheme, addr string) Transport {
  16. return httpTransport{
  17. RoundTripper: r,
  18. scheme: scheme,
  19. addr: addr,
  20. }
  21. }
  22. // NewRequest creates a new http.Request and sets the URL
  23. // scheme and address with the transport's fields.
  24. func (t httpTransport) NewRequest(path string, data io.Reader) (*http.Request, error) {
  25. req, err := newHTTPRequest(path, data)
  26. if err != nil {
  27. return nil, err
  28. }
  29. req.URL.Scheme = t.scheme
  30. req.URL.Host = t.addr
  31. return req, nil
  32. }