Skip to content

Commit

Permalink
DouniaBerrada: On behalf of DharaniGovindan, fixing InvalidCookieDoma…
Browse files Browse the repository at this point in the history
…inException thrown when trying to access cookies from a sub domain.

r12906
  • Loading branch information
Dounia Berrada committed Jul 12, 2011
1 parent e96a965 commit 38a747c
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions iphone/src/objc/Cookie.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ - (NSURL *)currentUrl {

- (void) deleteAllCookies {
NSHTTPCookieStorage* cookieStorage =
[NSHTTPCookieStorage sharedHTTPCookieStorage];
[NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* theCookies = [cookieStorage cookiesForURL:[self currentUrl]];
for (NSHTTPCookie *cookie in theCookies) {
[cookieStorage deleteCookie:cookie];
Expand All @@ -64,22 +64,22 @@ - (void) deleteAllCookies {

- (NSArray*) getCookies {
NSHTTPCookieStorage* cookieStorage =
[NSHTTPCookieStorage sharedHTTPCookieStorage];
[NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* theCookies = [cookieStorage cookiesForURL:[self currentUrl]];
NSMutableArray* toReturn = [NSMutableArray arrayWithCapacity:
[theCookies count]];
for (NSHTTPCookie *cookie in theCookies) {
NSMutableDictionary* cookieDict =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
[cookie name], @"name",
[cookie value], @"value",
[cookie domain], @"domain",
[cookie path], @"path",
[NSNumber numberWithBool:[cookie isSecure]], @"secure",
nil];
[NSMutableDictionary dictionaryWithObjectsAndKeys:
[cookie name], @"name",
[cookie value], @"value",
[cookie domain], @"domain",
[cookie path], @"path",
[NSNumber numberWithBool:[cookie isSecure]], @"secure",
nil];

NSDate* expires = [cookie expiresDate];
if (expires != nil) {
if ([expires isKindOfClass:[NSDate class]]) {
NSNumber* expiry =
[NSNumber numberWithLong:[expires timeIntervalSince1970]];
[cookieDict setObject:expiry forKey:@"expiry"];
Expand All @@ -93,13 +93,12 @@ - (void)addCookie:(NSDictionary *)cookie {
cookie = [cookie objectForKey:@"cookie"];
NSURL* currentUrl = [self currentUrl];
NSString* domain = [cookie objectForKey:@"domain"];

if (domain == nil) {
if (domain == (id)[NSNull null] || domain.length == 0) {
domain = [currentUrl host];
} else {
// Strip off the port if the domain has one.
domain = [[domain componentsSeparatedByString:@":"] objectAtIndex:0];
if (![[currentUrl host] isEqualToString:domain]) {
if (![[currentUrl host] hasSuffix:domain]) {
@throw [NSException webDriverExceptionWithMessage:
[NSString stringWithFormat:
@"You may only set cookies for the current domain:"
Expand All @@ -108,24 +107,22 @@ - (void)addCookie:(NSDictionary *)cookie {
}
}


NSString* path = [cookie objectForKey:@"path"];
if (path == nil) {
if (path == (id)[NSNull null] || path.length == 0) {
path = @"/";
}

// We need to convert the cookie data from the format used by WebDriver to one
// recognized by the NSHTTPCookie class.
NSMutableDictionary* cookieProperties =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
[cookie objectForKey:@"name"], NSHTTPCookieName,
[cookie objectForKey:@"value"], NSHTTPCookieValue,
domain, NSHTTPCookieDomain,
path, NSHTTPCookiePath,
nil];

[NSMutableDictionary dictionaryWithObjectsAndKeys:
[cookie objectForKey:@"name"], NSHTTPCookieName,
[cookie objectForKey:@"value"], NSHTTPCookieValue,
domain, NSHTTPCookieDomain,
path, NSHTTPCookiePath,
nil];
NSNumber* expires = [cookie objectForKey:@"expiry"];
if (expires != nil) {
if ([expires isKindOfClass:[NSNumber class]]) {
NSDate* expiresDate =
[NSDate dateWithTimeIntervalSince1970:[expires doubleValue]];
[cookieProperties setObject:expiresDate forKey:NSHTTPCookieExpires];
Expand Down Expand Up @@ -192,7 +189,7 @@ - (NSDictionary *)getCookie {
[NSNumber numberWithBool:[cookie isSecure]], @"secure",
nil];
NSDate* expires = [cookie expiresDate];
if (expires != nil) {
if ([expires isKindOfClass:[NSDate class]]) {
[cookieDict setObject:[expires description] forKey:@"expires"];
}
return cookieDict;
Expand Down

0 comments on commit 38a747c

Please sign in to comment.