BuildRequest creates a new [Request] with the provided context, URL, headers, and cookie jar
( ctx context.Context, url string, header http.Header, jar http.CookieJar, )
| 36 | |
| 37 | // BuildRequest creates a new [Request] with the provided context, URL, headers, and cookie jar |
| 38 | func (f *Fetcher) BuildRequest( |
| 39 | ctx context.Context, url string, header http.Header, jar http.CookieJar, |
| 40 | ) (*Request, error) { |
| 41 | url = transport.EscapeURL(url) |
| 42 | |
| 43 | // Set request timeout and get cancel function |
| 44 | ctx, cancel := context.WithTimeout(ctx, f.config.DownloadTimeout) |
| 45 | |
| 46 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 47 | if err != nil { |
| 48 | cancel() |
| 49 | return nil, newRequestError(err) |
| 50 | } |
| 51 | |
| 52 | // Check if the URL scheme is supported |
| 53 | if !f.transport.IsProtocolRegistered(req.URL.Scheme) { |
| 54 | cancel() |
| 55 | return nil, newRequestSchemeError(req.URL.Scheme) |
| 56 | } |
| 57 | |
| 58 | // Add cookies from the jar to the request (if any) |
| 59 | if jar != nil { |
| 60 | for _, cookie := range jar.Cookies(req.URL) { |
| 61 | req.AddCookie(cookie) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Set user agent header |
| 66 | req.Header.Set(httpheaders.UserAgent, f.config.UserAgent) |
| 67 | |
| 68 | // Set headers |
| 69 | httpheaders.CopyToRequest(header, req) |
| 70 | |
| 71 | return &Request{f, req, cancel}, nil |
| 72 | } |
| 73 | |
| 74 | // checkRedirect is a method that checks if the number of redirects exceeds the maximum allowed |
| 75 | func (f *Fetcher) checkRedirect(req *http.Request, via []*http.Request) error { |
no test coverage detected