Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only follow redirect once #15

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,28 @@ var (
verboseMode bool = false
)

func getRemoteFileSize(url string) (int64, error) {
func getRemoteFileSize(url string) (string, int64, error) {
// TODO: this needs a retry
resp, err := http.DefaultClient.Head(url)
if err != nil {
return int64(-1), err
return "", int64(-1), err
}
defer resp.Body.Close()
trueUrl := resp.Request.URL.String()
if (trueUrl != url) {
fmt.Printf("Redirected to %s\n", trueUrl)
}

fileSize := resp.ContentLength
if fileSize <= 0 {
return int64(-1), fmt.Errorf("unable to determine file size")
return "", int64(-1), fmt.Errorf("unable to determine file size")
}
_fileSize = fileSize
return fileSize, nil
return trueUrl, fileSize, nil
}

func downloadFileToBuffer(url string, concurrency int, retries int) (*bytes.Buffer, error) {
fileSize, err := getRemoteFileSize(url)
trueUrl, fileSize, err := getRemoteFileSize(url)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -104,7 +109,7 @@ func downloadFileToBuffer(url string, concurrency int, retries int) (*bytes.Buff
CheckRedirect: checkRedirectFunc,
}

req, err := http.NewRequest("GET", url, nil)
req, err := http.NewRequest("GET", trueUrl, nil)
if err != nil {
// This needs to be a time.Duration to make everything happy
fmt.Printf("Error creating request: %v\n", err)
Expand Down Expand Up @@ -212,6 +217,7 @@ func main() {
retries := flag.Int("r", 5, "Number of retries when attempting to retreive file")
extract := flag.Bool("x", false, "extract tar file")
verbose := flag.Bool("v", false, "verbose mode")
force := flag.Bool("f", false, "force download, overwriting existing file")
flag.Parse()

// check required positional arguments
Expand All @@ -225,7 +231,7 @@ func main() {
dest := args[1]

// ensure dest does not exist
if _, err := os.Stat(dest); !os.IsNotExist(err) {
if _, err := os.Stat(dest); !*force || !os.IsNotExist(err) {
fmt.Printf("Destination %s already exists\n", dest)
os.Exit(1)
}
Expand Down