diff --git a/registry/customtransport.go b/registry/customtransport.go new file mode 100644 index 00000000..f2f50644 --- /dev/null +++ b/registry/customtransport.go @@ -0,0 +1,24 @@ +package registry + +import ( + "net/http" +) + +// CustomTransport defines the data structure for custom http.Request options. +type CustomTransport struct { + Transport http.RoundTripper + Headers map[string]string +} + +// RoundTrip defines the round tripper for the error transport. +func (t *CustomTransport) RoundTrip(request *http.Request) (*http.Response, error) { + if len(t.Headers) != 0 { + for header, value := range t.Headers { + request.Header.Add(header, value) + } + } + + resp, err := t.Transport.RoundTrip(request) + + return resp, err +} diff --git a/registry/registry.go b/registry/registry.go index c48c31fb..c9d27d54 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -45,6 +45,7 @@ type Opt struct { Debug bool SkipPing bool Timeout time.Duration + Headers map[string]string } // New creates a new Registry struct with the given URL and credentials. @@ -83,6 +84,10 @@ func newFromTransport(auth types.AuthConfig, transport http.RoundTripper, opt Op errorTransport := &ErrorTransport{ Transport: basicAuthTransport, } + customTransport := &CustomTransport{ + Transport: errorTransport, + Headers: opt.Headers, + } // set the logging logf := Quiet @@ -95,7 +100,7 @@ func newFromTransport(auth types.AuthConfig, transport http.RoundTripper, opt Op Domain: reProtocol.ReplaceAllString(url, ""), Client: &http.Client{ Timeout: opt.Timeout, - Transport: errorTransport, + Transport: customTransport, }, Username: auth.Username, Password: auth.Password,