Add custom http header (#104)

This commit is contained in:
Jan-Otto Kröpke 2018-07-08 15:43:09 +02:00 committed by Jess Frazelle
parent a8549a2c40
commit 74603b6d47
2 changed files with 30 additions and 1 deletions

View file

@ -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
}

View file

@ -45,6 +45,7 @@ type Opt struct {
Debug bool Debug bool
SkipPing bool SkipPing bool
Timeout time.Duration Timeout time.Duration
Headers map[string]string
} }
// New creates a new Registry struct with the given URL and credentials. // 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{ errorTransport := &ErrorTransport{
Transport: basicAuthTransport, Transport: basicAuthTransport,
} }
customTransport := &CustomTransport{
Transport: errorTransport,
Headers: opt.Headers,
}
// set the logging // set the logging
logf := Quiet logf := Quiet
@ -95,7 +100,7 @@ func newFromTransport(auth types.AuthConfig, transport http.RoundTripper, opt Op
Domain: reProtocol.ReplaceAllString(url, ""), Domain: reProtocol.ReplaceAllString(url, ""),
Client: &http.Client{ Client: &http.Client{
Timeout: opt.Timeout, Timeout: opt.Timeout,
Transport: errorTransport, Transport: customTransport,
}, },
Username: auth.Username, Username: auth.Username,
Password: auth.Password, Password: auth.Password,