Ben Copsey

Make user agent string generation work on Mac OS X

Added test for user agent
... ... @@ -397,6 +397,12 @@ extern NSString* const NetworkRequestErrorDomain;
+ (int)compressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath;
+ (int)compressDataFromSource:(FILE *)source toDestination:(FILE *)dest;
#pragma mark get user agent
// Will be used as a user agent if requests do not specify a custom user agent
// Is only used when you have specified a Bundle Display Name (CFDisplayBundleName) or Bundle Name (CFBundleName) in your plist
+ (NSString *)defaultUserAgentString;
@property (retain) NSString *username;
@property (retain) NSString *password;
@property (retain) NSString *domain;
... ...
... ... @@ -389,25 +389,14 @@ static NSError *ASITooMuchRedirectionError;
}
}
// Set a logical user agent (on iPhones, for now)
#if TARGET_OS_IPHONE
if ([[self requestHeaders] objectForKey:@"User-Agent"] == nil) {
UIDevice *device = [UIDevice currentDevice];
NSBundle *bundle = [NSBundle mainBundle];
NSLocale *locale = [NSLocale currentLocale];
NSString *userAgent = [NSString stringWithFormat:@"%@ %@ (%@; %@ %@; %@)",
[bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"],
[bundle objectForInfoDictionaryKey:@"CFBundleVersion"],
[device model],
[device systemName],
[device systemVersion],
[locale localeIdentifier]
];
[self addRequestHeader:@"User-Agent" value:userAgent];
// Build and set the user agent string if the request does not already have a custom user agent specified
if (![[self requestHeaders] objectForKey:@"User-Agent"]) {
NSString *userAgentString = [ASIHTTPRequest defaultUserAgentString];
if (userAgentString) {
[self addRequestHeader:@"User-Agent" value:userAgentString];
}
}
#endif
// Accept a compressed response
if ([self allowCompressedResponse]) {
... ... @@ -1869,6 +1858,53 @@ static NSError *ASITooMuchRedirectionError;
return Z_OK;
}
#pragma mark get user agent
+ (NSString *)defaultUserAgentString
{
NSBundle *bundle = [NSBundle mainBundle];
// Attempt to find a name for this application
NSString *appName = [bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
if (!appName) {
appName = [bundle objectForInfoDictionaryKey:@"CFBundleName"];
}
// If we couldn't find one, we'll give up (and ASIHTTPRequest will use the standard CFNetwork user agent)
if (!appName) {
return nil;
}
NSString *appVersion = [bundle objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *deviceName;;
NSString *OSName;
NSString *OSVersion;
NSString *locale = [[NSLocale currentLocale] localeIdentifier];
#if TARGET_OS_IPHONE
UIDevice *device = [UIDevice currentDevice];
deviceName = [device model];
OSName = [device systemName];
OSVersion = [device systemVersion];
#else
deviceName = @"Macintosh";
OSName = @"Mac OS X";
// From http://www.cocoadev.com/index.pl?DeterminingOSVersion
// We won't bother to check for systems prior to 10.4, since ASIHTTPRequest only works on 10.5+
OSErr err;
SInt32 versionMajor, versionMinor, versionBugFix;
if ((err = Gestalt(gestaltSystemVersionMajor, &versionMajor)) != noErr) return nil;
if ((err = Gestalt(gestaltSystemVersionMinor, &versionMinor)) != noErr) return nil;
if ((err = Gestalt(gestaltSystemVersionBugFix, &versionBugFix)) != noErr) return nil;
OSVersion = [NSString stringWithFormat:@"%u.%u.%u", versionMajor, versionMinor, versionBugFix];
#endif
// Takes the form "My Application 1.0 (Macintosh; Mac OS X 10.5.7; en_GB)"
return [NSString stringWithFormat:@"%@ %@ (%@; %@ %@; %@)", appName, appVersion, deviceName, OSName, OSVersion, locale];
}
@synthesize username;
@synthesize password;
... ...
... ... @@ -20,6 +20,7 @@
- (void)testTimeOut;
- (void)testRequestMethod;
- (void)testHTTPVersion;
- (void)testUserAgent;
- (void)testUploadContentLength;
- (void)testDownloadContentLength;
- (void)testFileDownload;
... ...
... ... @@ -132,6 +132,27 @@
GHAssertTrue(success,@"Wrong HTTP version used");
}
- (void)testUserAgent
{
// defaultUserAgentString will be nil if we haven't set a Bundle Name or Bundle Display Name
if ([ASIHTTPRequest defaultUserAgentString]) {
// Returns the user agent it received in the response body
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/user-agent"]];
[request start];
BOOL success = [[request responseString] isEqualToString:[ASIHTTPRequest defaultUserAgentString]];
GHAssertTrue(success,@"Failed to set the correct user agent");
}
// Now test specifying a custom user agent
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/user-agent"]];
[request addRequestHeader:@"User-Agent" value:@"Ferdinand Fuzzworth's Magic Tent of Mystery"];
[request start];
BOOL success = [[request responseString] isEqualToString:@"Ferdinand Fuzzworth's Magic Tent of Mystery"];
GHAssertTrue(success,@"Failed to set the correct user agent");
}
- (void)testAutomaticRedirection
{
ASIHTTPRequest *request;
... ...
... ... @@ -24,5 +24,7 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>CFBundleDisplayName</key>
<string>ASIHTTPRequest Demo</string>
</dict>
</plist>
... ...
... ... @@ -24,5 +24,7 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>CFBundleDisplayName</key>
<string>ASIHTTPRequest Unit Tests</string>
</dict>
</plist>
... ...
... ... @@ -9,7 +9,7 @@
<key>CFBundleIconFile</key>
<string>iphone-icon.png</string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.etc</string>
<string>com.yourcompany.${PRODUCT_NAME:identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
... ... @@ -20,5 +20,7 @@
<string>1.0</string>
<key>NSMainNibFile</key>
<string>MainWindow</string>
<key>CFBundleDisplayName</key>
<string>ASIHTTPRequest Demo</string>
</dict>
</plist>
... ...
... ... @@ -18,5 +18,7 @@
<string>1.0</string>
<key>NSMainNibFile</key>
<string>GHUnitIPhone</string>
<key>CFBundleDisplayName</key>
<string>ASIHTTPRequest Unit Tests</string>
</dict>
</plist>
... ...
This diff was suppressed by a .gitattributes entry.