URLUtils.m
changeset 17 a1044ae95953
parent 11 e5976864dfe9
child 19 5ade3e09a827
     1.1 --- a/URLUtils.m	Sat May 24 13:24:33 2008 -0700
     1.2 +++ b/URLUtils.m	Wed Jul 16 10:50:34 2008 -0700
     1.3 @@ -9,6 +9,64 @@
     1.4  #import "URLUtils.h"
     1.5  
     1.6  
     1.7 +@implementation NSURL (MYUtilities)
     1.8 +
     1.9 ++ (NSString*) my_stringByTrimmingURLString: (NSString*)string
    1.10 +{
    1.11 +    NSMutableString *trimmed = [[string mutableCopy] autorelease];
    1.12 +    NSRange r;
    1.13 +    // Remove all whitespace and newlines:
    1.14 +    while(YES){
    1.15 +        r = [trimmed rangeOfCharacterFromSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    1.16 +        if( r.length==0 )
    1.17 +            break;
    1.18 +        [trimmed replaceCharactersInRange: r withString: @""];
    1.19 +    }
    1.20 +    
    1.21 +    // Delete surrounding "<...>":
    1.22 +    r = NSMakeRange(0,trimmed.length);
    1.23 +    if( [trimmed hasPrefix: @"<"] ) {
    1.24 +        r.location++;
    1.25 +        r.length--;
    1.26 +    }
    1.27 +    if( [trimmed hasSuffix: @">"] )
    1.28 +        r.length--;
    1.29 +    return [trimmed substringWithRange: r];
    1.30 +}
    1.31 +
    1.32 ++ (NSURL*) my_URLWithLenientString: (NSString*)string 
    1.33 +                     defaultScheme: (NSString*)defaultScheme
    1.34 +                    allowedSchemes: (NSArray*)allowedSchemes
    1.35 +{
    1.36 +    // Trim it:
    1.37 +    string = [self my_stringByTrimmingURLString: string];
    1.38 +    if( string.length==0 )
    1.39 +        return nil;
    1.40 +    NSURL *url = [NSURL URLWithString: string];
    1.41 +    if( ! url )
    1.42 +        return nil;
    1.43 +    // Apply default scheme (if any):
    1.44 +    NSString *scheme = url.scheme.lowercaseString;
    1.45 +    if( scheme == nil ) {
    1.46 +        if(  ! defaultScheme )
    1.47 +            return nil;
    1.48 +        string = $sprintf(@"%@://%@", defaultScheme,string);
    1.49 +        url = [NSURL URLWithString: string];
    1.50 +        scheme = [url scheme];
    1.51 +        if( scheme == nil )
    1.52 +            return nil;
    1.53 +    }
    1.54 +    // Check that scheme is allowed:
    1.55 +    if( allowedSchemes && ![allowedSchemes containsObject: scheme] )
    1.56 +        return nil;
    1.57 +    return url;
    1.58 +}
    1.59 +
    1.60 +@end
    1.61 +
    1.62 +
    1.63 +
    1.64 +
    1.65  @implementation NSHTTPURLResponse (MYUtilities)
    1.66  
    1.67