| 1 |
package httpcli |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
type roundTripperFunc func(*http.Request) (*http.Response, error) |
| 9 |
|
| 10 |
|
| 11 |
func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) } |
| 12 |
|
| 13 |
|
| 14 |
func BasicAuth(username, passwd string) ConfigOpt { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
return WrapRoundTripper(func(rt http.RoundTripper) http.RoundTripper { |
| 19 |
return roundTripperFunc(func(req *http.Request) (*http.Response, error) { |
| 20 |
var h http.Header |
| 21 |
if req.Header != nil { |
| 22 |
h = make(http.Header, len(req.Header)) |
| 23 |
for k, v := range req.Header { |
| 24 |
h[k] = append(make([]string, 0, len(v)), v...) |
| 25 |
} |
| 26 |
} |
| 27 |
clonedReq := *req |
| 28 |
clonedReq.Header = h |
| 29 |
clonedReq.SetBasicAuth(username, passwd) |
| 30 |
return rt.RoundTrip(&clonedReq) |
| 31 |
}) |
| 32 |
}) |
| 33 |
} |