mirror of
https://github.com/genuinetools/reg.git
synced 2024-05-20 12:08:33 -04:00
Add custom http header (#104)
This commit is contained in:
parent
a8549a2c40
commit
74603b6d47
24
registry/customtransport.go
Normal file
24
registry/customtransport.go
Normal 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
|
||||||
|
}
|
|
@ -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,
|
||||||
|
|
Loading…
Reference in a new issue