From c899bccd43b5289d78583bda1af14300fce50c67 Mon Sep 17 00:00:00 2001 From: rfm Date: Fri, 17 Jan 2025 10:45:47 +0000 Subject: [PATCH] Coding style fixes and performance optimisation --- Source/NSHTTPCookie.m | 53 +++++++++++++++--------------- Source/NSHTTPCookieStorage.m | 62 +++++++++++++++++++----------------- 2 files changed, 61 insertions(+), 54 deletions(-) diff --git a/Source/NSHTTPCookie.m b/Source/NSHTTPCookie.m index 28e4bfda63..3c86ecaea6 100644 --- a/Source/NSHTTPCookie.m +++ b/Source/NSHTTPCookie.m @@ -280,13 +280,13 @@ - (NSDate *) expiresDate - (BOOL) _isValidProperty: (NSString *)prop { - return ([prop length] - && [prop rangeOfString: @"\n"].location == NSNotFound); + return ([prop length] && [prop rangeOfString: @"\n"].location == NSNotFound); } - (id) initWithProperties: (NSDictionary *)properties { NSMutableDictionary *rawProps; + if ((self = [super init]) == nil) return nil; @@ -301,7 +301,7 @@ - (id) initWithProperties: (NSDictionary *)properties return nil; } - rawProps = [[properties mutableCopy] autorelease]; + rawProps = AUTORELEASE([properties mutableCopy]); if ([rawProps objectForKey: @"Created"] == nil) { NSInteger seconds; @@ -317,13 +317,13 @@ - (id) initWithProperties: (NSDictionary *)properties } if ([rawProps objectForKey: NSHTTPCookieExpires] == nil || [[rawProps objectForKey: NSHTTPCookieExpires] - isKindOfClass: [NSDate class]] == NO) + isKindOfClass: [NSDate class]] == NO) { [rawProps setObject: [NSNumber numberWithBool: YES] forKey: NSHTTPCookieDiscard]; } - this->_properties = [rawProps copy]; + ASSIGNCOPY(this->_properties, rawProps); return self; } @@ -376,7 +376,7 @@ - (NSUInteger) version - (NSString *) description { return [NSString stringWithFormat: @"", self, - [self name], [self value]]; + [self name], [self value]]; } - (NSUInteger) hash @@ -620,8 +620,8 @@ static BOOL skipSpace(pldata *pld) obj = [NSString alloc]; obj = [obj initWithCharactersNoCopy: chars - length: length - freeWhenDone: YES]; + length: length + freeWhenDone: YES]; } pld->pos++; return obj; @@ -663,6 +663,8 @@ static BOOL skipSpace(pldata *pld) static BOOL _setCookieKey(NSMutableDictionary *dict, NSString *key, NSString *value) { + NSString *lKey; + if ([dict count] == 0) { /* This must be the name=value pair */ @@ -672,21 +674,22 @@ static BOOL skipSpace(pldata *pld) [dict setObject: value forKey: NSHTTPCookieValue]; return YES; } - if ([[key lowercaseString] isEqual: @"comment"]) + lKey = [key lowercaseString]; + if ([lKey isEqual: @"comment"]) [dict setObject: value forKey: NSHTTPCookieComment]; - else if ([[key lowercaseString] isEqual: @"commenturl"]) + else if ([lKey isEqual: @"commenturl"]) [dict setObject: value forKey: NSHTTPCookieCommentURL]; - else if ([[key lowercaseString] isEqual: @"discard"]) + else if ([lKey isEqual: @"discard"]) [dict setObject: [NSNumber numberWithBool: YES] forKey: NSHTTPCookieDiscard]; - else if ([[key lowercaseString] isEqual: @"domain"]) + else if ([lKey isEqual: @"domain"]) [dict setObject: value forKey: NSHTTPCookieDomain]; - else if ([[key lowercaseString] isEqual: @"expires"]) + else if ([lKey isEqual: @"expires"]) { - NSDate *expireDate; - NSDateFormatter *formatter; - NSLocale *locale; - NSTimeZone *gmtTimeZone; + NSDate *expireDate; + NSDateFormatter *formatter; + NSLocale *locale; + NSTimeZone *gmtTimeZone; locale = [NSLocale localeWithLocaleIdentifier: @"en_US"]; gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation: @"GMT"]; @@ -696,26 +699,26 @@ static BOOL skipSpace(pldata *pld) [formatter setLocale: locale]; [formatter setTimeZone: gmtTimeZone]; - expireDate = [formatter dateFromString:value]; + expireDate = [formatter dateFromString: value]; if (expireDate) [dict setObject: expireDate forKey: NSHTTPCookieExpires]; RELEASE(formatter); } - else if ([[key lowercaseString] isEqual: @"max-age"]) + else if ([lKey isEqual: @"max-age"]) [dict setObject: value forKey: NSHTTPCookieMaximumAge]; - else if ([[key lowercaseString] isEqual: @"originurl"]) + else if ([lKey isEqual: @"originurl"]) [dict setObject: value forKey: NSHTTPCookieOriginURL]; - else if ([[key lowercaseString] isEqual: @"path"]) + else if ([lKey isEqual: @"path"]) [dict setObject: value forKey: NSHTTPCookiePath]; - else if ([[key lowercaseString] isEqual: @"port"]) + else if ([lKey isEqual: @"port"]) [dict setObject: value forKey: NSHTTPCookiePort]; - else if ([[key lowercaseString] isEqual: @"secure"]) + else if ([lKey isEqual: @"secure"]) [dict setObject: [NSNumber numberWithBool: YES] forKey: NSHTTPCookieSecure]; - else if ([[key lowercaseString] isEqual: @"httponly"]) + else if ([lKey isEqual: @"httponly"]) [dict setObject: [NSNumber numberWithBool: YES] forKey: HTTPCookieHTTPOnly]; - else if ([[key lowercaseString] isEqual: @"version"]) + else if ([lKey isEqual: @"version"]) [dict setObject: value forKey: NSHTTPCookieVersion]; return YES; } diff --git a/Source/NSHTTPCookieStorage.m b/Source/NSHTTPCookieStorage.m index 37e46f8155..5e6a56e0b0 100644 --- a/Source/NSHTTPCookieStorage.m +++ b/Source/NSHTTPCookieStorage.m @@ -109,12 +109,12 @@ - (void) dealloc - (NSString *) _cookieStorePath { - BOOL isDir; - NSString *path; - NSArray *dirs; + BOOL isDir; + NSString *path; + NSArray *dirs; dirs = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, - NSUserDomainMask, YES); + NSUserDomainMask, YES); path = [[dirs objectAtIndex: 0] stringByAppendingPathComponent: @"Cookies"]; if ([[NSFileManager defaultManager] fileExistsAtPath: path isDirectory: &isDir] == NO || isDir == NO) @@ -136,17 +136,18 @@ - (NSString *) _cookieStorePath /* FIXME: When will we know that the user session expired? */ - (BOOL) _expireCookies: (BOOL)endUserSession { - BOOL changed = NO; - NSDate *now = [NSDate date]; - unsigned count = [this->_cookies count]; + BOOL changed = NO; + NSDate *now = [NSDate date]; + unsigned count = [this->_cookies count]; /* FIXME: Handle Max-age */ while (count-- > 0) { NSHTTPCookie *ck = [this->_cookies objectAtIndex: count]; - NSDate *expDate = [ck expiresDate]; - if ((endUserSession && expDate == nil) || - (expDate != nil && [expDate compare: now] != NSOrderedDescending)) + NSDate *expDate = [ck expiresDate]; + + if ((endUserSession && expDate == nil) + || (expDate != nil && [expDate compare: now] != NSOrderedDescending)) { [this->_cookies removeObject: ck]; changed = YES; @@ -157,9 +158,9 @@ - (BOOL) _expireCookies: (BOOL)endUserSession - (void) _updateFromCookieStore { - int i; - NSArray *properties; - NSString *path = [self _cookieStorePath]; + int i; + NSArray *properties; + NSString *path = [self _cookieStorePath]; if (path == nil) { @@ -192,9 +193,10 @@ - (void) _updateFromCookieStore - (void) _updateToCookieStore { - int i, count; - NSMutableArray *properties; - NSString *path = [self _cookieStorePath]; + int i; + int count; + NSMutableArray *properties; + NSString *path = [self _cookieStorePath]; if (path == nil) { @@ -237,15 +239,15 @@ - (NSHTTPCookieAcceptPolicy) cookieAcceptPolicy - (NSArray *) cookies { - return [[this->_cookies copy] autorelease]; + return AUTORELEASE([this->_cookies copy]); } - (NSArray *) cookiesForURL: (NSURL *)URL { - NSMutableArray *a = [NSMutableArray array]; - NSEnumerator *ckenum = [this->_cookies objectEnumerator]; - NSHTTPCookie *cookie; - NSString *receive_domain = [URL host]; + NSMutableArray *a = [NSMutableArray array]; + NSEnumerator *ckenum = [this->_cookies objectEnumerator]; + NSHTTPCookie *cookie; + NSString *receive_domain = [URL host]; while ((cookie = [ckenum nextObject])) { @@ -268,11 +270,12 @@ - (void) deleteCookie: (NSHTTPCookie *)cookie - (void) _setCookieNoNotify: (NSHTTPCookie *)cookie { - NSEnumerator *ckenum = [this->_cookies objectEnumerator]; - NSHTTPCookie *ck, *remove_ck; - NSString *name = [cookie name]; - NSString *path = [cookie path]; - NSString *domain = [cookie domain]; + NSEnumerator *ckenum = [this->_cookies objectEnumerator]; + NSHTTPCookie *ck; + NSHTTPCookie *remove_ck; + NSString *name = [cookie name]; + NSString *path = [cookie path]; + NSString *domain = [cookie domain]; NSAssert([cookie isKindOfClass: [NSHTTPCookie class]] == YES, NSInvalidArgumentException); @@ -331,9 +334,10 @@ - (void) setCookies: (NSArray *)cookies NSHTTPCookie *ck = [cookies objectAtIndex: count]; if (this->_policy == NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain - && [[URL host] hasSuffix: [mainDocumentURL host]] == NO) - continue; - + && [[URL host] hasSuffix: [mainDocumentURL host]] == NO) + { + continue; + } [self _setCookieNoNotify: ck]; changed = YES; }