Merge pull request #16141 from MattHardcastle/bad-status-code

Error bad status while downloading launcher
This commit is contained in:
Matt Hardcastle 2019-09-09 08:56:00 -07:00 committed by GitHub
commit 7e5026a365
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View file

@ -3,6 +3,8 @@
@interface DownloadLauncher : NSObject<NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLDownloadDelegate> {
}
@property (readonly) bool didBecomeDownloadTask;
- (void) downloadLauncher:(NSString*) launcherUrl;
@end

View file

@ -8,6 +8,13 @@ static const NSString *kIOError = @"IOError";
@implementation DownloadLauncher
-(id)init {
if ((self = [super init]) != nil) {
_didBecomeDownloadTask = false;
}
return self;
}
- (void) downloadLauncher:(NSString*)launcherUrl {
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:launcherUrl]
cachePolicy:NSURLRequestUseProtocolCachePolicy
@ -16,8 +23,8 @@ static const NSString *kIOError = @"IOError";
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDownloadTask *downloadTask = [defaultSession downloadTaskWithRequest:request];
[downloadTask resume];
NSURLSessionDataTask *task = [defaultSession dataTaskWithRequest:request];
[task resume];
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
@ -45,6 +52,25 @@ static const NSString *kIOError = @"IOError";
}
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSURLSessionResponseDisposition disposition = NSURLSessionResponseBecomeDownload;
if (httpResponse.statusCode != 200) {
NSLog(@"expected statusCode 200, got %ld", (long)httpResponse.statusCode);
disposition = NSURLSessionResponseCancel;
}
completionHandler(disposition);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask
{
_didBecomeDownloadTask = true;
}
-(void)URLSession:(NSURLSession*)session downloadTask:(NSURLSessionDownloadTask*)downloadTask didFinishDownloadingToURL:(NSURL*)location {
NSLog(@"Did finish downloading to url");
@try {
@ -95,9 +121,14 @@ static const NSString *kIOError = @"IOError";
}
- (void)URLSession:(NSURLSession*)session task:(NSURLSessionTask*)task didCompleteWithError:(NSError*)error {
NSLog(@"completed; error: %@", error);
if (error) {
if (_didBecomeDownloadTask && [task class] == [NSURLSessionDataTask class]) {
return;
}
NSLog(@"couldn't complete download: %@", error);
[[Launcher sharedLauncher] displayErrorPage];
} else {
NSLog(@"finished downloading Launcher");
}
}
@end