Skip to content

Commit

Permalink
Coding style fixes and performance optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Jan 17, 2025
1 parent 1d97721 commit c899bcc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 54 deletions.
53 changes: 28 additions & 25 deletions Source/NSHTTPCookie.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -376,7 +376,7 @@ - (NSUInteger) version
- (NSString *) description
{
return [NSString stringWithFormat: @"<NSHTTPCookie %p: %@=%@>", self,
[self name], [self value]];
[self name], [self value]];
}

- (NSUInteger) hash
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -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"];
Expand All @@ -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;
}
Expand Down
62 changes: 33 additions & 29 deletions Source/NSHTTPCookieStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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]))
{
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit c899bcc

Please sign in to comment.