github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/pkg/plugins/transport/http.go (about)

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