From d8fec158424707ce272a5bd9432b22c97ba037e8 Mon Sep 17 00:00:00 2001 From: Matt Hardcastle Date: Thu, 5 Sep 2019 14:50:20 -0700 Subject: [PATCH] Error bad status while downloading launcher --- launchers/darwin/src/DownloadLauncher.h | 2 ++ launchers/darwin/src/DownloadLauncher.m | 37 +++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/launchers/darwin/src/DownloadLauncher.h b/launchers/darwin/src/DownloadLauncher.h index 2f97910cd8..c1aeae4a5a 100644 --- a/launchers/darwin/src/DownloadLauncher.h +++ b/launchers/darwin/src/DownloadLauncher.h @@ -3,6 +3,8 @@ @interface DownloadLauncher : NSObject { } +@property (readonly) bool didBecomeDownloadTask; + - (void) downloadLauncher:(NSString*) launcherUrl; @end diff --git a/launchers/darwin/src/DownloadLauncher.m b/launchers/darwin/src/DownloadLauncher.m index c54227ba83..0811779db8 100644 --- a/launchers/darwin/src/DownloadLauncher.m +++ b/launchers/darwin/src/DownloadLauncher.m @@ -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