pokeb/asi-http-request — Issue #311: Incomplete file downloads (with status code 206)
Author: ghulammustafa
Created: 2012-03-07 04:51:28+00:00
State: open

I'm downloading mp3 files from Rackspace cloud files, and for large files i'm encountering an issue where the download is completed successfully but the file is not yet downloaded completely. For example, a 40 MB mp3 file (01:00:00 duration) is download as as 4.5 MB mp3 file (00:10:30 duration). This doesn't happen all the time.

Here's how i create and send an async request:

ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setShouldAttemptPersistentConnection:NO];
[request setAllowResumeForFileDownloads:YES];
[request setDownloadProgressDelegate:self];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setUserInfo:userInfo];
[request setDownloadDestinationPath:downloadPath];
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@.download", downloadPath]];

[self.networkQueue addOperation:request];
[self.networkQueue go];

Note i'm using a network queue with 4 concurrent downloads.

Further investigation shows that `ASINetworkQueue` is calling `requestDidFinishSelector` delegate method instead of `requestDidFailSelector`. The status code returned by the `ASIHTTPRequest` object is `206, HTTP/1.1 206 Partial Content` in `requestDidFinishSelector` method. The status code should be `200, HTTP/1.1 200 OK`.

I still don't know why! and i still don't know how to fix this. It seems that i'll have to delete the partially downloaded file and start the download process again. At this point the temporary file i.e. %@.download is removed, and this partially downloaded file is put at the destination path.

Any pointers as to what's going on?
Why is this happening, and how can i fix this issue?
How can i build a simple checksum logic to check if the file was downloaded completely?