From 74603b6d47a08e9f347442a7f787acbb70bf4883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Sun, 8 Jul 2018 15:43:09 +0200 Subject: [PATCH] Add custom http header (#104) --- registry/customtransport.go | 24 ++++++++++++++++++++++++ registry/registry.go | 7 ++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 registry/customtransport.go 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,