Avoid memory leaks by implementing hash and isEqual: for credentials

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@23088 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2006-06-19 15:37:50 +00:00
parent 83f8003fc2
commit 8b55e40030
4 changed files with 48 additions and 4 deletions

View file

@ -72,6 +72,13 @@ typedef enum {
password: (NSString *)password password: (NSString *)password
persistence: (NSURLCredentialPersistence)persistence; persistence: (NSURLCredentialPersistence)persistence;
/**
* Tests two credentials for equality ... credentials are considered to
* be equal if their -user methods return the same value, since you cannot
* have more than one credential for a suser within an [NSURLProtectionSpace].
*/
- (BOOL) isEqual: (id)other;
/** /**
* Returns the password for the receiver.<br /> * Returns the password for the receiver.<br />
* May require prompting of the user to authorize retrieval.<br /> * May require prompting of the user to authorize retrieval.<br />

View file

@ -25,11 +25,13 @@
#include "GSURLPrivate.h" #include "GSURLPrivate.h"
#include "Foundation/NSDictionary.h" #include "Foundation/NSDictionary.h"
#include "Foundation/NSScanner.h" #include "Foundation/NSScanner.h"
#include "Foundation/NSSet.h"
#include "Foundation/NSDebug.h" #include "Foundation/NSDebug.h"
#include "GNUstepBase/GSLock.h" #include "GNUstepBase/GSLock.h"
#include "GNUstepBase/GSMime.h" #include "GNUstepBase/GSMime.h"
static NSMutableSet *spaces = nil;
static NSMutableDictionary *store = nil; static NSMutableDictionary *store = nil;
static GSLazyLock *storeLock = nil; static GSLazyLock *storeLock = nil;
static GSMimeParser *mimeParser = nil; static GSMimeParser *mimeParser = nil;
@ -75,6 +77,7 @@ static GSMimeParser *mimeParser = nil;
if (store == nil) if (store == nil)
{ {
mimeParser = [GSMimeParser new]; mimeParser = [GSMimeParser new];
spaces = [NSMutableSet new];
store = [NSMutableDictionary new]; store = [NSMutableDictionary new];
storeLock = [GSLazyLock new]; storeLock = [GSLazyLock new];
} }
@ -84,9 +87,21 @@ static GSMimeParser *mimeParser = nil;
inProtectionSpace: (NSURLProtectionSpace*)space inProtectionSpace: (NSURLProtectionSpace*)space
{ {
NSMutableDictionary *cDict; NSMutableDictionary *cDict;
NSURLProtectionSpace *known;
GSHTTPDigest *digest = nil; GSHTTPDigest *digest = nil;
[storeLock lock]; [storeLock lock];
/*
* Keep track of known protection spaces so we don't make lots of
* duplicate copies, but share one copy between digest objects.
*/
known = [spaces member: space];
if (known == nil)
{
[spaces addObject: space];
known = [spaces member: space];
}
space = known;
cDict = [store objectForKey: space]; cDict = [store objectForKey: space];
if (cDict == nil) if (cDict == nil)
{ {
@ -318,8 +333,8 @@ static GSMimeParser *mimeParser = nil;
if ((self = [super init]) != nil) if ((self = [super init]) != nil)
{ {
self->_lock = [GSLazyLock new]; self->_lock = [GSLazyLock new];
ASSIGNCOPY(self->_space, space); ASSIGN(self->_space, space);
ASSIGNCOPY(self->_credential, credential); ASSIGN(self->_credential, credential);
} }
return self; return self;
} }

View file

@ -91,6 +91,11 @@ typedef struct {
return this->hasPassword; return this->hasPassword;
} }
- (unsigned) hash
{
return [this->user hash];
}
- (id) initWithUser: (NSString *)user - (id) initWithUser: (NSString *)user
password: (NSString *)password password: (NSString *)password
persistence: (NSURLCredentialPersistence)persistence persistence: (NSURLCredentialPersistence)persistence
@ -114,6 +119,19 @@ typedef struct {
return self; return self;
} }
- (BOOL) isEqual: (id)other
{
if ((id)self == other)
{
return YES;
}
if ([other isKindOfClass: [NSURLCredential class]] == NO)
{
return NO;
}
return [[(NSURLCredential*)other user] isEqualToString: this->user];
}
- (NSString *) password - (NSString *) password
{ {
return this->password; return this->password;

View file

@ -162,6 +162,10 @@ authenticationMethod: (NSString *)authenticationMethod
- (unsigned) isEqual: (id)other - (unsigned) isEqual: (id)other
{ {
if ((id)self == other)
{
return YES;
}
if ([other isKindOfClass: [NSURLProtectionSpace class]] == NO) if ([other isKindOfClass: [NSURLProtectionSpace class]] == NO)
{ {
return NO; return NO;
@ -202,8 +206,8 @@ authenticationMethod: (NSString *)authenticationMethod
return NO; return NO;
} }
} }
return YES; return YES;
} }
} }
- (BOOL) isProxy - (BOOL) isProxy