Skip to content

Commit

Permalink
Merge pull request #1873 from sparkle-project/http-headers-releasenot…
Browse files Browse the repository at this point in the history
…es-1x

Pass http headers and user agent when downloading release notes (1.x)
  • Loading branch information
zorgiepoo authored Jun 20, 2021
2 parents c0933a5 + 99785ce commit c4fcf17
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sparkle/SUUIBasedUpdateDriver.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ - (void)didFindValidUpdate
return;
}

self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:updateItem host:self.host completionBlock:^(SUUpdateAlertChoice choice) {
self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:updateItem httpHeaders:updater.httpHeaders userAgent:updater.userAgentString host:self.host completionBlock:^(SUUpdateAlertChoice choice) {
[self updateAlertFinishedWithChoice:choice forItem:updateItem];
}];

Expand Down
2 changes: 1 addition & 1 deletion Sparkle/SUUpdateAlert.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef NS_ENUM(NSInteger, SUUpdateAlertChoice) {

@property (weak) id<SUVersionDisplay> versionDisplayer;

- (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)host completionBlock:(void(^)(SUUpdateAlertChoice))c;
- (instancetype)initWithAppcastItem:(SUAppcastItem *)item httpHeaders:(NSDictionary *)httpHeaders userAgent:(NSString *)userAgent host:(SUHost *)host completionBlock:(void(^)(SUUpdateAlertChoice))c;

- (IBAction)installUpdate:sender;
- (IBAction)skipThisVersion:sender;
Expand Down
25 changes: 23 additions & 2 deletions Sparkle/SUUpdateAlert.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ @interface SUUpdateAlert () <NSTouchBarDelegate>

@property (nonatomic) id<SUWebView> webView;

@property (nonatomic) NSDictionary *httpHeaders;
@property (nonatomic) NSString *userAgent;

@end

@implementation SUUpdateAlert
Expand All @@ -74,14 +77,19 @@ @implementation SUUpdateAlert

@synthesize webView = _webView;

- (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)aHost completionBlock:(void (^)(SUUpdateAlertChoice))block
@synthesize httpHeaders = _httpHeaders;
@synthesize userAgent = _userAgent;

- (instancetype)initWithAppcastItem:(SUAppcastItem *)item httpHeaders:(NSDictionary *)httpHeaders userAgent:(NSString *)userAgent host:(SUHost *)aHost completionBlock:(void (^)(SUUpdateAlertChoice))block
{
self = [super initWithWindowNibName:@"SUUpdateAlert"];
if (self)
{
self.completionBlock = block;
host = aHost;
updateItem = item;
_httpHeaders = httpHeaders;
_userAgent = userAgent;
[self setShouldCascadeWindows:NO];
}
return self;
Expand Down Expand Up @@ -145,8 +153,21 @@ - (void)displayReleaseNotes
// If there's a release notes URL, load it; otherwise, just stick the contents of the description into the web view.
if ([self.updateItem releaseNotesURL])
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self.updateItem releaseNotesURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

if (self.userAgent) {
[request setValue:self.userAgent forHTTPHeaderField:@"User-Agent"];
}

if (self.httpHeaders) {
for (NSString *key in self.httpHeaders) {
NSString *value = [self.httpHeaders objectForKey:key];
[request setValue:value forHTTPHeaderField:key];
}
}

__weak __typeof__(self) weakSelf = self;
[self.webView loadRequest:[NSURLRequest requestWithURL:[self.updateItem releaseNotesURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30] completionHandler:^(NSError * _Nullable error) {
[self.webView loadRequest:request completionHandler:^(NSError * _Nullable error) {
if (error != nil) {
SULog(SULogLevelError, @"Failed to load URL request from web view: %@", error);
}
Expand Down

0 comments on commit c4fcf17

Please sign in to comment.