Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make user agent string generation work on Mac OS X

Added test for user agent
  • Loading branch information...
commit 8d36fda37af35ca68680d7d2c4d29f58463f5e00 1 parent 237aa8d
@pokeb authored
View
6 Classes/ASIHTTPRequest.h
@@ -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;
View
72 Classes/ASIHTTPRequest.m
@@ -389,25 +389,14 @@ - (void)main
}
}
- // 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 @@ + (int)compressDataFromSource:(FILE *)source toDestination:(FILE *)dest
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;
View
1  Classes/Tests/ASIHTTPRequestTests.h
@@ -20,6 +20,7 @@
- (void)testTimeOut;
- (void)testRequestMethod;
- (void)testHTTPVersion;
+- (void)testUserAgent;
- (void)testUploadContentLength;
- (void)testDownloadContentLength;
- (void)testFileDownload;
View
21 Classes/Tests/ASIHTTPRequestTests.m
@@ -132,6 +132,27 @@ - (void)testHTTPVersion
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;
View
2  Mac Sample/Info.plist
@@ -24,5 +24,7 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
+ <key>CFBundleDisplayName</key>
+ <string>ASIHTTPRequest Demo</string>
</dict>
</plist>
View
2  Mac Sample/Tests-Info.plist
@@ -24,5 +24,7 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
+ <key>CFBundleDisplayName</key>
+ <string>ASIHTTPRequest Unit Tests</string>
</dict>
</plist>
View
4 iPhone Sample/Info.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>
View
2  iPhone Sample/Tests-Info.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>
View
6 iPhone.xcodeproj/project.pbxproj
@@ -429,7 +429,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
- CODE_SIGN_IDENTITY = "iPhone Developer";
+ CODE_SIGN_IDENTITY = "Don't Code Sign";
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
@@ -450,6 +450,7 @@
);
PREBINDING = NO;
PRODUCT_NAME = "Unit Tests (GHUnit)";
+ PROVISIONING_PROFILE = "";
SDKROOT = iphoneos3.0;
};
name = Debug;
@@ -458,7 +459,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
- CODE_SIGN_IDENTITY = "iPhone Developer";
+ CODE_SIGN_IDENTITY = "Don't Code Sign";
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_ENABLE_FIX_AND_CONTINUE = NO;
@@ -478,6 +479,7 @@
);
PREBINDING = NO;
PRODUCT_NAME = "Unit Tests (GHUnit)";
+ PROVISIONING_PROFILE = "";
SDKROOT = iphoneos3.0;
ZERO_LINK = NO;
};
Please sign in to comment.
Something went wrong with that request. Please try again.