Skip to content

Commit 6d58fc0

Browse files
committed
Fix Downloader callback bug on iOS9
On iOS 9 when server returns a response with HTTP status 403 caused the downloader to callback successfully and saving the error message to the storage path. This commit adds a check on the HTTP status code and do failure callback if status code is 4xx or 5xx.
1 parent f97a110 commit 6d58fc0

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

cocos/network/CCDownloader-apple.mm

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,18 @@ - (void)URLSession:(NSURLSession *)session downloadTask :(NSURLSessionDownloadTa
515515
NSString *destPath = [NSString stringWithUTF8String:storagePath];
516516
NSFileManager *fileManager = [NSFileManager defaultManager];
517517
NSURL *destURL = nil;
518-
518+
519+
// On iOS 9 a response with status code 4xx(Client Error) or 5xx(Server Error)
520+
// might end up calling this delegate method, saving the error message to the storage path
521+
// and treating this download task as a successful one, so we need to check the status code here
522+
NSInteger statusCode = ((NSHTTPURLResponse*)downloadTask.response).statusCode;
523+
if (statusCode >= 400) {
524+
std::vector<unsigned char> buf; // just a placeholder
525+
std::string response = [[NSString stringWithContentsOfURL:location] UTF8String] ?: "";
526+
_outer->onTaskFinish(*[wrapper get], cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL, noErr, response, buf);
527+
return;
528+
}
529+
519530
do
520531
{
521532
if ([destPath hasPrefix:@"file://"])

0 commit comments

Comments
 (0)