Skip to content

Commit

Permalink
Provide the ability to set a custom user agent string on all HTTP req…
Browse files Browse the repository at this point in the history
…uests that Sparkle initiates.
  • Loading branch information
bdash committed Aug 21, 2009
1 parent 150718a commit dfe0eff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
9 changes: 4 additions & 5 deletions SUBasicUpdateDriver.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ - (void)checkForUpdatesAtURL:(NSURL *)URL host:(SUHost *)aHost
[appcast release];

[appcast setDelegate:self];
NSString *userAgent = [NSString stringWithFormat: @"%@/%@ Sparkle/%@", [aHost name], [aHost displayVersion], ([SPARKLE_BUNDLE objectForInfoDictionaryKey:@"CFBundleVersion"] ?: nil)];
NSData * cleanedAgent = [userAgent dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
userAgent = [[[NSString alloc] initWithData:cleanedAgent encoding:NSASCIIStringEncoding] autorelease];
[appcast setUserAgentString:userAgent];
[appcast setUserAgentString:[updater userAgentString]];
[appcast fetchAppcastFromURL:URL];
}

Expand Down Expand Up @@ -128,7 +125,9 @@ - (void)didNotFindUpdate

- (void)downloadUpdate
{
download = [[NSURLDownload alloc] initWithRequest:[NSURLRequest requestWithURL:[updateItem fileURL]] delegate:self];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[updateItem fileURL]];
[request setValue:[updater userAgentString] forHTTPHeaderField:@"User-Agent"];
download = [[NSURLDownload alloc] initWithRequest:request delegate:self];
}

- (void)download:(NSURLDownload *)d decideDestinationWithSuggestedFilename:(NSString *)name
Expand Down
6 changes: 5 additions & 1 deletion SUUpdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
@interface SUUpdater : NSObject {
NSTimer *checkTimer;
SUUpdateDriver *driver;


NSString *customUserAgentString;
SUHost *host;
IBOutlet id delegate;
}
Expand All @@ -38,6 +39,9 @@
- (void)setFeedURL:(NSURL *)feedURL;
- (NSURL *)feedURL;

- (void)setUserAgentString:(NSString *)userAgent;
- (NSString *)userAgentString;

- (void)setSendsSystemProfile:(BOOL)sendsSystemProfile;
- (BOOL)sendsSystemProfile;

Expand Down
19 changes: 19 additions & 0 deletions SUUpdater.m
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,25 @@ - (NSURL *)feedURL
return [NSURL URLWithString:[appcastString stringByTrimmingCharactersInSet:quoteSet]];
}

- (void)setUserAgentString:(NSString *)userAgent
{
if (customUserAgentString == userAgent)
return;

[customUserAgentString release];
customUserAgentString = [userAgent copy];
}

- (NSString *)userAgentString
{
if (customUserAgentString)
return customUserAgentString;

NSString *userAgent = [NSString stringWithFormat:@"%@/%@ Sparkle/%@", [host name], [host displayVersion], [SPARKLE_BUNDLE objectForInfoDictionaryKey:@"CFBundleVersion"] ?: nil];
NSData *cleanedAgent = [userAgent dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
return [[[NSString alloc] initWithData:cleanedAgent encoding:NSASCIIStringEncoding] autorelease];
}

- (void)setSendsSystemProfile:(BOOL)sendsSystemProfile
{
[host setBool:sendsSystemProfile forUserDefaultsKey:SUSendProfileInfoKey];
Expand Down

0 comments on commit dfe0eff

Please sign in to comment.