From 3f226ccdd757281755aedeff85a30303a29ffab1 Mon Sep 17 00:00:00 2001 From: Geraint White Date: Sat, 6 Feb 2021 21:41:04 +0000 Subject: [PATCH] Apply custom headers to app download The custom headers added in #489 only applied to the appcast download. This makes them apply to the app download as well for the case when both are hosted behind authentication. --- Sparkle/SPUCoreBasedUpdateDriver.m | 5 ++++- Sparkle/SPUDownloadDriver.h | 2 +- Sparkle/SPUDownloadDriver.m | 9 ++++++++- Sparkle/SUUpdater.h | 4 ++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Sparkle/SPUCoreBasedUpdateDriver.m b/Sparkle/SPUCoreBasedUpdateDriver.m index 83cb55b4..8dbfb9a0 100644 --- a/Sparkle/SPUCoreBasedUpdateDriver.m +++ b/Sparkle/SPUCoreBasedUpdateDriver.m @@ -41,6 +41,7 @@ @property (nonatomic, readonly, weak) id updater; // if we didn't have legacy support, I'd remove this.. @property (nullable, nonatomic, readonly, weak) id updaterDelegate; @property (nonatomic) NSString *userAgent; +@property (nonatomic, nullable) NSDictionary *httpHeaders; @end @@ -58,6 +59,7 @@ @synthesize updater = _updater; @synthesize updaterDelegate = _updaterDelegate; @synthesize userAgent = _userAgent; +@synthesize httpHeaders = _httpHeaders; @synthesize resumableUpdate = _resumableUpdate; - (instancetype)initWithHost:(SUHost *)host applicationBundle:(NSBundle *)applicationBundle sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(nullable id )updaterDelegate delegate:(id)delegate @@ -111,6 +113,7 @@ - (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates requiresSilentInstall:(BOOL)silentInstall { self.userAgent = userAgent; + self.httpHeaders = httpHeaders; self.silentInstall = silentInstall; [self.basicDriver checkForUpdatesAtAppcastURL:appcastURL withUserAgent:userAgent httpHeaders:httpHeaders inBackground:background includesSkippedUpdates:includesSkippedUpdates]; @@ -167,7 +170,7 @@ - (void)downloadUpdateFromAppcastItem:(SUAppcastItem *)updateItem inBackground:(BOOL)background { - self.downloadDriver = [[SPUDownloadDriver alloc] initWithUpdateItem:updateItem host:self.host userAgent:self.userAgent inBackground:background delegate:self]; + self.downloadDriver = [[SPUDownloadDriver alloc] initWithUpdateItem:updateItem host:self.host userAgent:self.userAgent httpHeaders:self.httpHeaders inBackground:background delegate:self]; if ([self.updaterDelegate respondsToSelector:@selector((updater:willDownloadUpdate:withRequest:))]) { [self.updaterDelegate updater:self.updater diff --git a/Sparkle/SPUDownloadDriver.h b/Sparkle/SPUDownloadDriver.h index ec7bd581..910d39bb 100644 --- a/Sparkle/SPUDownloadDriver.h +++ b/Sparkle/SPUDownloadDriver.h @@ -28,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SPUDownloadDriver : NSObject -- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem host:(SUHost *)host userAgent:(NSString *)userAgent inBackground:(BOOL)background delegate:(id)delegate; +- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem host:(SUHost *)host userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate; - (void)downloadUpdate; diff --git a/Sparkle/SPUDownloadDriver.m b/Sparkle/SPUDownloadDriver.m index 37658a03..aee88dc2 100644 --- a/Sparkle/SPUDownloadDriver.m +++ b/Sparkle/SPUDownloadDriver.m @@ -53,7 +53,7 @@ @synthesize expectedContentLength = _expectedContentLength; @synthesize cleaningUp = _cleaningUp; -- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem host:(SUHost *)host userAgent:(NSString *)userAgent inBackground:(BOOL)background delegate:(id)delegate +- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem host:(SUHost *)host userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate { self = [super init]; if (self != nil) { @@ -65,6 +65,13 @@ _request = [NSMutableURLRequest requestWithURL:updateItem.fileURL]; [_request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; _request.networkServiceType = background ? NSURLNetworkServiceTypeBackground : NSURLNetworkServiceTypeDefault; + + if (httpHeaders) { + for (NSString *key in httpHeaders) { + NSString *value = [httpHeaders objectForKey:key]; + [_request setValue:value forHTTPHeaderField:key]; + } + } if (!SPUXPCServiceExists(@DOWNLOADER_BUNDLE_ID)) { _downloader = [[SPUDownloader alloc] initWithDelegate:self]; diff --git a/Sparkle/SUUpdater.h b/Sparkle/SUUpdater.h index b75c4449..9d7bebe8 100644 --- a/Sparkle/SUUpdater.h +++ b/Sparkle/SUUpdater.h @@ -154,14 +154,14 @@ SU_EXPORT @interface SUUpdater : NSObject @property (nonatomic, readonly) NSBundle *sparkleBundle; /*! - The user agent used when checking for updates. + The user agent used when checking for and downloading updates. The default implementation can be overrided. */ @property (nonatomic, copy) NSString *userAgentString; /*! - The HTTP headers used when checking for updates. + The HTTP headers used when checking for and downloading updates. The keys of this dictionary are HTTP header fields (NSString) and values are corresponding values (NSString) */