Changeset 5441

Show
Ignore:
Timestamp:
03/19/08 16:09:29 (7 months ago)
Author:
morris
Message:

Change the API for string parsing a bit

Handle IPv6 addresses enclosed in []

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • WiredAdditions/trunk/WIURL.h

    r5102 r5441  
    5454- (void)setScheme:(NSString *)value; 
    5555- (NSString *)scheme; 
     56- (void)setHostpair:(NSString *)value; 
     57- (NSString *)hostpair; 
    5658- (void)setHost:(NSString *)value; 
    5759- (NSString *)host; 
    58 - (NSString *)hostpair; 
    5960- (void)setPort:(NSUInteger)value; 
    6061- (NSUInteger)port; 
  • WiredAdditions/trunk/WIURL.m

    r5102 r5441  
    4545        if(!portmap) { 
    4646                portmap = [[NSDictionary alloc] initWithObjectsAndKeys: 
    47                         [NSNumber numberWithInt:2000],                @"wired", 
    48                         [NSNumber numberWithInt:2002],                @"wiredtracker", 
     47                        [NSNumber numberWithInt:4871],                @"wired", 
     48                        [NSNumber numberWithInt:4871],                @"wiredtracker", 
    4949                        NULL]; 
    5050        } 
     
    7373        string = [string stringByReplacingURLPercentEscapes]; 
    7474 
    75         // --- find SCHEME://host/ 
    7675        range = [string rangeOfString:@"://"]; 
    7776 
     
    8382        } 
    8483 
    85         // --- find scheme://host/PATH 
    8684        range = [string rangeOfString:@"/"]; 
    8785 
     
    9391        } 
    9492 
    95         // --- find scheme://USER@host/ 
    9693        range = [string rangeOfString:@"@"]; 
    9794 
     
    103100                string = [string substringFromIndex:range.location + 1]; 
    104101 
    105                 // --- find scheme://user:PASSWORD@host/ 
    106102                range = [auth rangeOfString:@":"]; 
    107103 
     
    128124                return NULL; 
    129125 
    130         url = [self URLWithScheme:scheme hostpair:string]; 
     126        url = [[self alloc] init]; 
     127        [url setScheme:scheme]; 
     128        [url setHostpair:string]; 
    131129        [url setUser:user]; 
    132130        [url setPassword:password]; 
     
    144142        } 
    145143 
    146         return url
     144        return [url autorelease]
    147145} 
    148146 
     
    150148 
    151149+ (id)URLWithScheme:(NSString *)scheme hostpair:(NSString *)hostpair { 
    152         NSString                *host; 
    153         NSRange                 range; 
    154         NSUInteger              port; 
    155  
    156         if([[hostpair componentsSeparatedByString:@":"] count] == 2) { 
    157                 range = [hostpair rangeOfString:@":" options:NSBackwardsSearch]; 
    158  
    159                 if(range.location == NSNotFound || 
    160                    range.location == 0 || 
    161                    range.location == [hostpair length] - 1) { 
    162                         host = hostpair; 
    163                         port = 0; 
    164                 } else { 
    165                         host = [hostpair substringToIndex:range.location]; 
    166                         port = [[hostpair substringFromIndex:range.location + 1] unsignedIntValue]; 
    167                 } 
    168         } else { 
    169                 host = hostpair; 
    170                 port = 0; 
    171         } 
    172  
    173         if(port == 0) 
    174                 port = [[[WIURL _portmap] objectForKey:scheme] unsignedIntValue]; 
    175  
    176         return [[[self alloc] initWithScheme:scheme host:host port:port] autorelease]; 
     150        WIURL           *url; 
     151         
     152        url = [[self alloc] init]; 
     153        [url setScheme:scheme]; 
     154        [url setHostpair:hostpair]; 
     155         
     156        return [url autorelease]; 
    177157} 
    178158 
     
    342322        if([[self host] length] > 0) { 
    343323                if([[self user] length] > 0 && ![[self user] isEqualToString:@"guest"]) 
    344                         [string appendFormat:@"%@:@", [self user]]; 
     324                        [string appendFormat:@"%@@", [self user]]; 
    345325 
    346326                [string appendString:[self hostpair]]; 
     
    400380 
    401381 
     382- (void)setHostpair:(NSString *)value { 
     383        NSString                *host; 
     384        NSRange                 range; 
     385        NSUInteger              port; 
     386         
     387        if([value hasPrefix:@"["] && [value containsSubstring:@"]"]) { 
     388                value   = [value substringFromIndex:1]; 
     389                range   = [value rangeOfString:@"]" options:NSBackwardsSearch]; 
     390                host    = [value substringToIndex:range.location]; 
     391                port    = 0; 
     392                 
     393                if([value containsSubstring:@"]:"]) { 
     394                        range = [value rangeOfString:@"]:" options:NSBackwardsSearch]; 
     395                         
     396                        if(range.location != [value length] - 2) 
     397                                port = [[value substringFromIndex:range.location + 2] unsignedIntValue]; 
     398                } 
     399        } 
     400        else if([[value componentsSeparatedByString:@":"] count] == 2) { 
     401                range = [value rangeOfString:@":" options:NSBackwardsSearch]; 
     402 
     403                if(range.location == NSNotFound || range.location == 0 || 
     404                   range.location == [value length] - 1) { 
     405                        host = value; 
     406                        port = 0; 
     407                } else { 
     408                        host = [value substringToIndex:range.location]; 
     409                        port = [[value substringFromIndex:range.location + 1] unsignedIntValue]; 
     410                } 
     411        } else { 
     412                host = value; 
     413                port = 0; 
     414        } 
     415         
     416    if(port == 0) 
     417        port = [[[WIURL _portmap] objectForKey:_scheme] unsignedIntValue]; 
     418 
     419        [self setHost:host]; 
     420        [self setPort:port]; 
     421} 
     422 
     423 
     424 
     425- (NSString *)hostpair { 
     426        if([[[WIURL _portmap] objectForKey:[self scheme]] unsignedIntValue] == [self port]) 
     427                return [self host]; 
     428 
     429        return [NSSWF:@"%@:%lu", [self host], [self port]]; 
     430} 
     431 
     432 
     433 
    402434- (void)setHost:(NSString *)value { 
    403435        [value retain]; 
     
    411443- (NSString *)host { 
    412444        return _host; 
    413 } 
    414  
    415  
    416  
    417 - (NSString *)hostpair { 
    418         if([[[WIURL _portmap] objectForKey:[self scheme]] unsignedIntValue] == [self port]) 
    419                 return [self host]; 
    420  
    421         return [NSSWF:@"%@:%lu", [self host], [self port]]; 
    422445} 
    423446