Skip to content

fix: Do not perform a GET request (with a zero-length range) if the file is already downloaded - #14

Merged
cmaglie merged 3 commits into
mainfrom
refactoring
Jan 5, 2026
Merged

cmaglie merged 3 commits into
mainfrom
refactoring

Conversation

@cmaglie

@cmaglie cmaglie commented Dec 19, 2025

Copy link
Copy Markdown
Member

As suggested in this PR.

@cmaglie cmaglie self-assigned this Dec 19, 2025
@cmaglie cmaglie added bug Something isn't working enhancement New feature or request labels Dec 19, 2025
@cmaglie cmaglie changed the title feat: Do not perform a GET request (with a zero-length range) if the file is already downloaded fix: Do not perform a GET request (with a zero-length range) if the file is already downloaded Dec 19, 2025
Base automatically changed from refactoring-2 to main December 19, 2025 15:54
Comment thread downloader.go Resolved
Comment thread downloader.go
Comment on lines +91 to +96
d.completedLock.Lock()
skip := (d.completed == d.size)
d.completedLock.Unlock()
if skip {
return d.Error()
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? Isn't size and completed both zero the first time?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size is the size of the remote file, it will be > 0.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering what happens if the server does NOT report the content-size... 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

field ContentLength int64

ContentLength records the length of the associated content. The value -1 indicates that the length is unknown. Unless Request.Method is "HEAD", values >= 0 indicate that the given number of bytes may be read from Body.

So, theoretically, we could also have -1 in the size field, mmmmmhhhh...

@cmaglie cmaglie Dec 19, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a fix to handle also the case with the server not providing Content-Lenght.
There are actually 4 cases:

Range= header accetpted Content-Len provided action
N N Restart from beginning (cannot use Range=...)
N Y Restart from beginning (cannot use Range=...)
Y N Always resume download
Restart from beginning
Y Y Resume download only if local file is smaller

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the Range= no Content-Len= is not correct. In that case you cannot resume the download because if you do a range query with a size that is the fully downloaded file, you will get a 416 Range Not Satisfiable. I think that you can safely resume only if you have both headers.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There you go

@cmaglie
cmaglie merged commit 908c131 into main Jan 5, 2026
3 checks passed
@cmaglie
cmaglie deleted the refactoring branch January 5, 2026 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants