diff -r e5976864dfe9 -r 5a71993a1a70 URLUtils.m --- a/URLUtils.m Sat May 24 13:24:33 2008 -0700 +++ b/URLUtils.m Sat Mar 28 09:36:46 2009 -0700 @@ -9,6 +9,64 @@ #import "URLUtils.h" +@implementation NSURL (MYUtilities) + ++ (NSString*) my_stringByTrimmingURLString: (NSString*)string +{ + NSMutableString *trimmed = [[string mutableCopy] autorelease]; + NSRange r; + // Remove all whitespace and newlines: + while(YES){ + r = [trimmed rangeOfCharacterFromSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; + if( r.length==0 ) + break; + [trimmed replaceCharactersInRange: r withString: @""]; + } + + // Delete surrounding "<...>": + r = NSMakeRange(0,trimmed.length); + if( [trimmed hasPrefix: @"<"] ) { + r.location++; + r.length--; + } + if( [trimmed hasSuffix: @">"] ) + r.length--; + return [trimmed substringWithRange: r]; +} + ++ (NSURL*) my_URLWithLenientString: (NSString*)string + defaultScheme: (NSString*)defaultScheme + allowedSchemes: (NSArray*)allowedSchemes +{ + // Trim it: + string = [self my_stringByTrimmingURLString: string]; + if( string.length==0 ) + return nil; + NSURL *url = [NSURL URLWithString: string]; + if( ! url ) + return nil; + // Apply default scheme (if any): + NSString *scheme = url.scheme.lowercaseString; + if( scheme == nil ) { + if( ! defaultScheme ) + return nil; + string = $sprintf(@"%@://%@", defaultScheme,string); + url = [NSURL URLWithString: string]; + scheme = [url scheme]; + if( scheme == nil ) + return nil; + } + // Check that scheme is allowed: + if( allowedSchemes && ![allowedSchemes containsObject: scheme] ) + return nil; + return url; +} + +@end + + + + @implementation NSHTTPURLResponse (MYUtilities)