http: allow custom User-Agent for outgoing HTTP requests

This commit is contained in:
Srigovind Nayak authored and Michael Eischer committed 2024-05-30 15:38:06 +02:00
1 parent cdd210185d
commit de7b418bbe
6 files changed
+100

No files matched your search

@@ -0,0 +1,25 @@
package backend
import "net/http"
// httpUserAgentRoundTripper is a custom http.RoundTripper that modifies the User-Agent header
// of outgoing HTTP requests.
type httpUserAgentRoundTripper struct {
userAgent string
rt http.RoundTripper
}
func newCustomUserAgentRoundTripper(rt http.RoundTripper, userAgent string) *httpUserAgentRoundTripper {
return &httpUserAgentRoundTripper{
rt: rt,
userAgent: userAgent,
}
}
// RoundTrip modifies the User-Agent header of the request and then delegates the request
// to the underlying RoundTripper.
func (c *httpUserAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req = req.Clone(req.Context())
req.Header.Set("User-Agent", c.userAgent)
return c.rt.RoundTrip(req)
}