Skip to content

fix: fail parallel download when a segment is not a partial response - #554

Open
jayhemnani9910 wants to merge 1 commit into
imroc:masterfrom
jayhemnani9910:fix/parallel-download-status
Open

jayhemnani9910 wants to merge 1 commit into
imroc:masterfrom
jayhemnani9910:fix/parallel-download-status

Conversation

@jayhemnani9910

Copy link
Copy Markdown

ParallelDownload writes a wrong file and returns nil when a segment request doesn't come back as a partial response. With a 3,000,000-byte file, SetConcurrency(3) and SetSegmentSize(1 << 20):

server before after
http.FileServer (Range supported) correct file correct file
python3 -m http.server (no Range support) err = nil, 9,000,000 bytes (the whole file once per segment) download segment 1048576-2097151: unexpected status 200 OK
one segment answers 503 err = nil, the error page spliced into the file unexpected status 503 Service Unavailable
the URL returns 404 err = nil, the 404 page saved as the file bad status: 404 Not Found

handleTask only checked the request error, which is nil for any HTTP status, so whatever body came back was merged into the output. Do() also accepted an error status on the HEAD request and used its Content-Length as the file size.

Each segment now has to be 206 Partial Content. A 200 is accepted when there is only one segment, since the full body is then the right content. Do() returns an error when the HEAD request gets an error status.

Added parallel_download_test.go with httptest servers: one with Range support (file matches), one without (error, and a single-segment download still works), and a 404. It fails without the change. go test ./... passes, also with -race for the new test. Also downloaded a 3 MB file from python3 -m http.server: 9,000,000 bytes and nil before, an error after.

handleTask only checked the request error, so any status was merged into the file: a server without range support sent the whole file for every segment (a 3 MB file came out as 9 MB), an error page from one segment was spliced in, and a 404 page was saved as the file, all with Do() returning nil. Require 206 for each segment (200 is fine for a single segment) and an OK status on the HEAD request.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant