Fix problems with sites which require authentication if we don't have any

credentials


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@24752 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2007-03-02 11:53:20 +00:00
parent 7c6c308075
commit 4a89c7967e
3 changed files with 150 additions and 108 deletions

View file

@ -1,3 +1,10 @@
2007-03-02 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSHTTPURLHandle.m: Cope with nil authentication info
* Source/GSHTTPAuthentication.m: Cope with nil credential and
catch exceptions in lock protected areas (there shouldn't be
any, but best to be safe).
2007-03-01 Nicola Pero <nicola.pero@meta-innovation.com> 2007-03-01 Nicola Pero <nicola.pero@meta-innovation.com>
* Source/NSBundle.m ([NSBundle +bundleForLibrary:]): Fixed paths * Source/NSBundle.m ([NSBundle +bundleForLibrary:]): Fixed paths

View file

@ -100,40 +100,50 @@ static GSMimeParser *mimeParser = nil;
NSInvalidArgumentException); NSInvalidArgumentException);
[storeLock lock]; [storeLock lock];
NS_DURING
/*
* Keep track of known protection spaces so we don't make lots of
* duplicate copies, but share one copy between authentication objects.
*/
known = [spaces member: space];
if (known == nil)
{ {
[spaces addObject: space]; /*
* Keep track of known protection spaces so we don't make lots of
* duplicate copies, but share one copy between authentication objects.
*/
known = [spaces member: space]; known = [spaces member: space];
} if (known == nil)
space = known; {
cDict = [store objectForKey: space]; [spaces addObject: space];
if (cDict == nil) known = [spaces member: space];
{ }
cDict = [NSMutableDictionary new]; space = known;
[store setObject: cDict forKey: space]; cDict = [store objectForKey: space];
RELEASE(cDict); if (cDict == nil)
} {
authentication = [cDict objectForKey: credential]; cDict = [NSMutableDictionary new];
[store setObject: cDict forKey: space];
RELEASE(cDict);
}
authentication = [cDict objectForKey: credential];
if (authentication == nil) if (authentication == nil)
{ {
authentication = [[GSHTTPAuthentication alloc] authentication = [[GSHTTPAuthentication alloc]
initWithCredential: credential initWithCredential: credential
inProtectionSpace: space]; inProtectionSpace: space];
[cDict setObject: authentication forKey: [authentication credential]]; if (authentication != nil)
{
[cDict setObject: authentication
forKey: [authentication credential]];
RELEASE(authentication);
}
}
AUTORELEASE(RETAIN(authentication));
} }
else NS_HANDLER
{ {
RETAIN(authentication); [storeLock unlock];
[localException raise];
} }
NS_ENDHANDLER
[storeLock unlock]; [storeLock unlock];
return AUTORELEASE(authentication); return authentication;
} }
+ (NSURLProtectionSpace*) protectionSpaceForAuthentication: (NSString*)auth + (NSURLProtectionSpace*) protectionSpaceForAuthentication: (NSString*)auth
@ -219,14 +229,9 @@ static GSMimeParser *mimeParser = nil;
+ (NSURLProtectionSpace *) protectionSpaceForURL: (NSURL*)URL + (NSURLProtectionSpace *) protectionSpaceForURL: (NSURL*)URL
{ {
NSURLProtectionSpace *space = nil; NSURLProtectionSpace *space = nil;
NSString *found = nil;
NSString *scheme; NSString *scheme;
NSNumber *port; NSNumber *port;
NSString *server; NSString *server;
NSDictionary *sDict;
NSArray *keys;
unsigned count;
NSString *path;
scheme = [URL scheme]; scheme = [URL scheme];
port = [URL port]; port = [URL port];
@ -248,39 +253,52 @@ static GSMimeParser *mimeParser = nil;
server = [NSString stringWithFormat: @"%@://%@:%@", server = [NSString stringWithFormat: @"%@://%@:%@",
scheme, [URL host], port]; scheme, [URL host], port];
} }
[storeLock lock];
sDict = [domainMap objectForKey: server];
keys = [sDict allKeys];
count = [keys count];
path = [URL path];
while (count-- > 0)
{
NSString *key = [keys objectAtIndex: count];
unsigned kl = [key length];
if (found == nil || kl > [found length]) [storeLock lock];
{ NS_DURING
if (kl == 0 || [path hasPrefix: key] == YES) {
NSString *found = nil;
NSDictionary *sDict;
NSArray *keys;
unsigned count;
NSString *path;
sDict = [domainMap objectForKey: server];
keys = [sDict allKeys];
count = [keys count];
path = [URL path];
while (count-- > 0)
{
NSString *key = [keys objectAtIndex: count];
unsigned kl = [key length];
if (found == nil || kl > [found length])
{ {
found = key; if (kl == 0 || [path hasPrefix: key] == YES)
{
found = key;
}
} }
} }
if (found != nil)
{
space = AUTORELEASE(RETAIN([sDict objectForKey: found]));
}
} }
if (found != nil) NS_HANDLER
{ {
space = RETAIN([sDict objectForKey: found]); [storeLock unlock];
[localException raise];
} }
NS_ENDHANDLER
[storeLock unlock]; [storeLock unlock];
return AUTORELEASE(space); return space;
} }
+ (void) setProtectionSpace: (NSURLProtectionSpace *)space + (void) setProtectionSpace: (NSURLProtectionSpace *)space
forDomains: (NSArray*)domains forDomains: (NSArray*)domains
baseURL: (NSURL*)base baseURL: (NSURL*)base
{ {
NSEnumerator *e;
NSString *domain;
/* /*
* If there are no URIs specified, everything on the * If there are no URIs specified, everything on the
* host of the base URL is in the protection space * host of the base URL is in the protection space
@ -291,56 +309,67 @@ static GSMimeParser *mimeParser = nil;
} }
[storeLock lock]; [storeLock lock];
e = [domains objectEnumerator]; NS_DURING
while ((domain = [e nextObject]) != nil)
{ {
NSURL *u; NSEnumerator *e = [domains objectEnumerator];
NSString *path; NSString *domain;
NSNumber *port;
NSString *scheme;
NSString *server;
NSMutableDictionary *sDict;
u = [NSURL URLWithString: domain]; while ((domain = [e nextObject]) != nil)
scheme = [u scheme]; {
if (scheme == nil) NSURL *u;
{ NSString *path;
u = [NSURL URLWithString: domain relativeToURL: base]; NSNumber *port;
scheme = [u scheme]; NSString *scheme;
NSString *server;
NSMutableDictionary *sDict;
u = [NSURL URLWithString: domain];
scheme = [u scheme];
if (scheme == nil)
{
u = [NSURL URLWithString: domain relativeToURL: base];
scheme = [u scheme];
}
port = [u port];
if ([port intValue] == 80 && [scheme isEqualToString: @"http"])
{
port = nil;
}
else if ([port intValue] == 443 && [scheme isEqualToString: @"https"])
{
port = nil;
}
path = [u path];
if (path == nil)
{
path = @"";
}
if ([port intValue] == 0)
{
server = [NSString stringWithFormat: @"%@://%@",
scheme, [u host]];
}
else
{
server = [NSString stringWithFormat: @"%@://%@:%@",
scheme, [u host], port];
}
sDict = [domainMap objectForKey: server];
if (sDict == nil)
{
sDict = [NSMutableDictionary new];
[domainMap setObject: sDict forKey: server];
RELEASE(sDict);
}
[sDict setObject: space forKey: [u path]];
} }
port = [u port];
if ([port intValue] == 80 && [scheme isEqualToString: @"http"])
{
port = nil;
}
else if ([port intValue] == 443 && [scheme isEqualToString: @"https"])
{
port = nil;
}
path = [u path];
if (path == nil)
{
path = @"";
}
if ([port intValue] == 0)
{
server = [NSString stringWithFormat: @"%@://%@",
scheme, [u host]];
}
else
{
server = [NSString stringWithFormat: @"%@://%@:%@",
scheme, [u host], port];
}
sDict = [domainMap objectForKey: server];
if (sDict == nil)
{
sDict = [NSMutableDictionary new];
[domainMap setObject: sDict forKey: server];
RELEASE(sDict);
}
[sDict setObject: space forKey: [u path]];
} }
NS_HANDLER
{
[storeLock unlock];
[localException raise];
}
NS_ENDHANDLER
[storeLock unlock]; [storeLock unlock];
} }

View file

@ -452,19 +452,20 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
NSURLCredential *cred; NSURLCredential *cred;
NSString *method; NSString *method;
/* /* Create credential from user and password stored in the URL.
* Create credential from user and password * Returns nil if we have no username or password.
* stored in the URL.
*/ */
cred = [[NSURLCredential alloc] cred = [[NSURLCredential alloc]
initWithUser: [u user] initWithUser: [u user]
password: [u password] password: [u password]
persistence: NSURLCredentialPersistenceForSession]; persistence: NSURLCredentialPersistenceForSession];
/* Create authentication from credential ... returns nil if
* we have no credential.
*/
authentication = [GSHTTPAuthentication authentication = [GSHTTPAuthentication
authenticationWithCredential: cred authenticationWithCredential: cred
inProtectionSpace: space]; inProtectionSpace: space];
RELEASE(cred); RELEASE(cred);
method = [request objectForKey: GSHTTPPropertyMethodKey]; method = [request objectForKey: GSHTTPPropertyMethodKey];
@ -483,8 +484,12 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
auth = [authentication authorizationForAuthentication: nil auth = [authentication authorizationForAuthentication: nil
method: method method: method
path: [u path]]; path: [u path]];
/* If authentication is nil then auth will also be nil
NSMapInsert(wProperties, (void*)@"Authorization", (void*)auth); */
if (auth != nil)
{
[self writeProperty: auth forKey: @"Authorization"];
}
} }
} }
@ -634,7 +639,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
NSString *ac; NSString *ac;
GSHTTPAuthentication *authentication; GSHTTPAuthentication *authentication;
NSString *method; NSString *method;
NSString *a; NSString *auth;
ac = [ah value]; ac = [ah value];
space = [GSHTTPAuthentication space = [GSHTTPAuthentication
@ -646,6 +651,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
/* /*
* Create credential from user and password * Create credential from user and password
* stored in the URL. * stored in the URL.
* Returns nil if we have no username or password.
*/ */
cred = [[NSURLCredential alloc] cred = [[NSURLCredential alloc]
initWithUser: [url user] initWithUser: [url user]
@ -655,11 +661,11 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
/* /*
* Get the digest object and ask it for a header * Get the digest object and ask it for a header
* to use for authorisation. * to use for authorisation.
* Returns nil if we have no credential.
*/ */
authentication = [GSHTTPAuthentication authentication = [GSHTTPAuthentication
authenticationWithCredential: cred authenticationWithCredential: cred
inProtectionSpace: space]; inProtectionSpace: space];
RELEASE(cred); RELEASE(cred);
} }
else else
@ -680,12 +686,12 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
} }
} }
a = [authentication authorizationForAuthentication: ac auth = [authentication authorizationForAuthentication: ac
method: method method: method
path: [url path]]; path: [url path]];
if (a != nil) if (auth != nil)
{ {
[self writeProperty: a forKey: @"Authorization"]; [self writeProperty: auth forKey: @"Authorization"];
[self _tryLoadInBackground: u]; [self _tryLoadInBackground: u];
return; // Retrying. return; // Retrying.
} }