🧱 Chunk is a download manager for slow and unstable servers
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Adds user-agent customization option

+29
+3
cmd/chunk/main.go
··· 48 48 chunk.ChunkSize = chunkSize 49 49 chunk.RestartDownloads = restartDownloads 50 50 chunk.ProgressDir = progressDir 51 + chunk.UserAgent = userAgent 51 52 prog := newProgress() 52 53 for status := range chunk.Download(args...) { 53 54 if status.Error != nil { ··· 69 70 waitBetweenRetries time.Duration 70 71 restartDownloads bool 71 72 progressDir string 73 + userAgent string 72 74 ) 73 75 74 76 func init() { ··· 86 88 "", 87 89 fmt.Sprintf("directory where to track progress; %s under user's home directory if blank, or CHUNK_DIR environment variable", chunk.DefaultChunkDir), 88 90 ) 91 + rootCmd.Flags().StringVarP(&userAgent, "user-agent", "u", chunk.DefaultUserAgent, "user agent to use in HTTP requests") 89 92 } 90 93 91 94 func main() {
+11
downloader.go
··· 22 22 DefaultChunkSize = 8192 23 23 DefaultWaitRetry = 1 * time.Second 24 24 DefaultRestartDownload = false 25 + DefaultUserAgent = "" 25 26 ) 26 27 27 28 // DownloadStatus is the data propagated via the channel sent back to the user ··· 99 100 // ProgressDir is the directory where Chunk keeps track of each chunk 100 101 // downloaded of each file. 101 102 ProgressDir string 103 + 104 + // UserAgent is the user agent used for all requests. If not set, no 105 + // user agent is sent. 106 + UserAgent string 102 107 } 103 108 104 109 type chunk struct{ start, end int64 } ··· 110 115 req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) 111 116 if err != nil { 112 117 return nil, fmt.Errorf("error creating the request for %s: %w", u, err) 118 + } 119 + if d.UserAgent != "" { 120 + req.Header.Set("User-Agent", d.UserAgent) 113 121 } 114 122 req.Header.Set("Range", c.rangeHeader()) 115 123 resp, err := d.Client.Do(req) ··· 162 170 req, err := http.NewRequestWithContext(ctx, http.MethodHead, u, nil) 163 171 if err != nil { 164 172 return fmt.Errorf("creating the request for %s: %w", u, err) 173 + } 174 + if d.UserAgent != "" { 175 + req.Header.Add("User-Agent", d.UserAgent) 165 176 } 166 177 resp, err := d.Client.Do(req) 167 178 if err != nil {
+15
downloader_test.go
··· 299 299 } 300 300 } 301 301 302 + func TestGetDownload_WithUserAgent(t *testing.T) { 303 + ua := "Answer/42.0" 304 + s := httptest.NewServer(http.HandlerFunc( 305 + func(w http.ResponseWriter, r *http.Request) { 306 + if r.UserAgent() != ua { 307 + t.Errorf("expected user-agent to be %s, got %s", ua, r.UserAgent()) 308 + } 309 + }, 310 + )) 311 + defer s.Close() 312 + d := DefaultDownloader() 313 + d.UserAgent = ua 314 + <-d.Download(s.URL) 315 + } 316 + 302 317 func TestGetDownloadSize_ContentLength(t *testing.T) { 303 318 s := httptest.NewServer(http.HandlerFunc( 304 319 func(w http.ResponseWriter, r *http.Request) {