From db3e6029b9b25f287787e625a1d3401a2b817b7d Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 4 Jan 2024 20:51:36 +0100 Subject: [PATCH] Add option to set http user agent --- client.go | 9 +++++++++ cmd/deadlinks/main.go | 15 +++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 81cc0d5..6cedd0f 100644 --- a/client.go +++ b/client.go @@ -36,6 +36,11 @@ type ClientOpts struct { Do(*http.Request) (*http.Response, error) } + // HTTPUserAgent overwrites the user agent used by the HTTPClient. + // + // Defaults to whatever http.Client uses by default. + HTTPUserAgent string + // MaxRedirects indicates the maximum number of redirects which will be // allowed when resolving a resource. A negative value indicates no // redirects are allowed. @@ -190,6 +195,10 @@ func (c *client) getHTTP( return "", nil, fmt.Errorf("building request: %w", err) } + if c.opts.HTTPUserAgent != "" { + req.Header.Set("User-Agent", c.opts.HTTPUserAgent) + } + res, err := c.opts.HTTPClient.Do(req) if err != nil { return "", nil, fmt.Errorf("performing request: %w", err) diff --git a/cmd/deadlinks/main.go b/cmd/deadlinks/main.go index 767c9ff..552dd65 100644 --- a/cmd/deadlinks/main.go +++ b/cmd/deadlinks/main.go @@ -31,11 +31,12 @@ func (c loggingClient) Get( func main() { var ( - storePath = flag.String("store-path", "", "Path to sqlite storage file. If not given then a temporary in-memory storage is used") - maxAge = flag.Duration("max-age", 0, "Maximum duration since last check of a resource, before it must be checked again. Must be used with -store-path") - urls = flag.String("urls", "", `Comma-separated list of URLs which are always checked. At least one is required`) - patternsStr = flag.String("patterns", "", "Comma-separated list of regexps. All URLs which match one of these will have their links checked as well") - concurrency = flag.Int("concurrency", runtime.NumCPU()/2, "Number simultaneous requests to make at a time") + storePath = flag.String("store-path", "", "Path to sqlite storage file. If not given then a temporary in-memory storage is used") + maxAge = flag.Duration("max-age", 0, "Maximum duration since last check of a resource, before it must be checked again. Must be used with -store-path") + urls = flag.String("urls", "", `Comma-separated list of URLs which are always checked. At least one is required`) + patternsStr = flag.String("patterns", "", "Comma-separated list of regexps. All URLs which match one of these will have their links checked as well") + concurrency = flag.Int("concurrency", runtime.NumCPU()/2, "Number simultaneous requests to make at a time") + httpUserAgent = flag.String("http-user-agent", "", "User-agent to use for http requests") ) flag.Parse() @@ -64,7 +65,9 @@ func main() { patterns, &deadlinks.Opts{ NewClient: func() deadlinks.Client { - return loggingClient{deadlinks.NewClient(nil)} + return loggingClient{deadlinks.NewClient(&deadlinks.ClientOpts{ + HTTPUserAgent: *httpUserAgent, + })} }, Concurrency: *concurrency, OnError: func(err error) {