custom user-agent transport wrapper (#21483)

This commit is contained in:
M Alvee
2025-08-08 23:51:53 +06:00
committed by GitHub
parent b44b2a090c
commit 02ba581ecf
3 changed files with 26 additions and 2 deletions

View File

@@ -183,3 +183,23 @@ func (s ConnSettings) NewRemoteTargetHTTPTransport(insecure bool) func() *http.T
return tr
}
}
// uaTransport - User-Agent transport
type uaTransport struct {
ua string
rt http.RoundTripper
}
func (u *uaTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req2 := req.Clone(req.Context())
req2.Header.Set("User-Agent", u.ua)
return u.rt.RoundTrip(req2)
}
// WithUserAgent wraps an existing transport with custom User-Agent
func WithUserAgent(rt http.RoundTripper, getUA func() string) http.RoundTripper {
return &uaTransport{
ua: getUA(),
rt: rt,
}
}