sendRequest is a common logic between sync and async download.
( ctx context.Context, url string, opts DownloadOptions, )
| 195 | |
| 196 | // sendRequest is a common logic between sync and async download. |
| 197 | func (f *Factory) sendRequest( |
| 198 | ctx context.Context, url string, opts DownloadOptions, |
| 199 | ) (*fetcher.Request, *http.Response, http.Header, error) { |
| 200 | h := make(http.Header) |
| 201 | |
| 202 | // NOTE: This will be removed in the future when our test context gets better isolation |
| 203 | if len(redirectAllRequestsTo) > 0 { |
| 204 | url = redirectAllRequestsTo |
| 205 | } |
| 206 | |
| 207 | header := opts.Header |
| 208 | |
| 209 | // Inject monitoring headers. |
| 210 | // Clone the headers to avoid modifying the original ones. |
| 211 | if f.monitoring != nil { |
| 212 | header = header.Clone() |
| 213 | f.monitoring.InjectHeaders(ctx, header) |
| 214 | } |
| 215 | |
| 216 | req, err := f.fetcher.BuildRequest(ctx, url, header, opts.CookieJar) |
| 217 | if err != nil { |
| 218 | return req, nil, h, err |
| 219 | } |
| 220 | |
| 221 | res, err := req.Fetch() |
| 222 | if res != nil { |
| 223 | h = res.Header.Clone() |
| 224 | } |
| 225 | if err != nil { |
| 226 | if res != nil { |
| 227 | res.Body.Close() |
| 228 | } |
| 229 | req.Cancel() |
| 230 | |
| 231 | return req, nil, h, err |
| 232 | } |
| 233 | |
| 234 | res, err = limitResponseSize(res, opts.MaxSrcFileSize) |
| 235 | if err != nil { |
| 236 | if res != nil { |
| 237 | res.Body.Close() |
| 238 | } |
| 239 | req.Cancel() |
| 240 | |
| 241 | return req, nil, h, err |
| 242 | } |
| 243 | |
| 244 | return req, res, h, nil |
| 245 | } |
| 246 | |
| 247 | func (f *Factory) startMonitoringSpan( |
| 248 | ctx context.Context, |
no test coverage detected