Debian Codegraph @

api/v1/lib/httpcli/auth_basic.go

raw symbols SCIP
1 package httpcli
2
3 import (
4 "net/http"
5 )
6
7 // roundTripperFunc is the functional adaptation of http.RoundTripper
8 type roundTripperFunc func(*http.Request) (*http.Response, error)
9
10 // RoundTrip implements RoundTripper for roundTripperFunc
11 func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) }
12
13 // BasicAuth generates a functional config option that sets HTTP Basic authentication for a Client
14 func BasicAuth(username, passwd string) ConfigOpt {
15 // TODO(jdef) this could be more efficient. according to the stdlib we're not supposed to
16 // mutate the original Request, so we copy here (including headers). another approach would
17 // be to generate a functional RequestOpt that adds the right header.
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 }