URLUtils.m
author Olivier Scherler <oscherler@femto-byte.com>
Tue May 12 14:38:30 2009 +0200 (2009-05-12)
changeset 30 2befbe36c746
parent 17 a1044ae95953
permissions -rw-r--r--
Changed -[MYDirectoryEvent relativePath] to work on standardised paths, in case symlinks are used. Fixes issue #28 in Murky.
jens@7
     1
//
jens@7
     2
//  URLUtils.m
jens@11
     3
//  MYUtilities
jens@7
     4
//
jens@7
     5
//  Created by Jens Alfke on 4/28/08.
jens@11
     6
//  Copyright 2008 Jens Alfke. All rights reserved.
jens@7
     7
//
jens@7
     8
jens@7
     9
#import "URLUtils.h"
jens@7
    10
jens@7
    11
jens@17
    12
@implementation NSURL (MYUtilities)
jens@17
    13
jens@17
    14
+ (NSString*) my_stringByTrimmingURLString: (NSString*)string
jens@17
    15
{
jens@17
    16
    NSMutableString *trimmed = [[string mutableCopy] autorelease];
jens@17
    17
    NSRange r;
jens@17
    18
    // Remove all whitespace and newlines:
jens@17
    19
    while(YES){
jens@17
    20
        r = [trimmed rangeOfCharacterFromSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
jens@17
    21
        if( r.length==0 )
jens@17
    22
            break;
jens@17
    23
        [trimmed replaceCharactersInRange: r withString: @""];
jens@17
    24
    }
jens@17
    25
    
jens@17
    26
    // Delete surrounding "<...>":
jens@17
    27
    r = NSMakeRange(0,trimmed.length);
jens@17
    28
    if( [trimmed hasPrefix: @"<"] ) {
jens@17
    29
        r.location++;
jens@17
    30
        r.length--;
jens@17
    31
    }
jens@17
    32
    if( [trimmed hasSuffix: @">"] )
jens@17
    33
        r.length--;
jens@17
    34
    return [trimmed substringWithRange: r];
jens@17
    35
}
jens@17
    36
jens@17
    37
+ (NSURL*) my_URLWithLenientString: (NSString*)string 
jens@17
    38
                     defaultScheme: (NSString*)defaultScheme
jens@17
    39
                    allowedSchemes: (NSArray*)allowedSchemes
jens@17
    40
{
jens@17
    41
    // Trim it:
jens@17
    42
    string = [self my_stringByTrimmingURLString: string];
jens@17
    43
    if( string.length==0 )
jens@17
    44
        return nil;
jens@17
    45
    NSURL *url = [NSURL URLWithString: string];
jens@17
    46
    if( ! url )
jens@17
    47
        return nil;
jens@17
    48
    // Apply default scheme (if any):
jens@17
    49
    NSString *scheme = url.scheme.lowercaseString;
jens@17
    50
    if( scheme == nil ) {
jens@17
    51
        if(  ! defaultScheme )
jens@17
    52
            return nil;
jens@17
    53
        string = $sprintf(@"%@://%@", defaultScheme,string);
jens@17
    54
        url = [NSURL URLWithString: string];
jens@17
    55
        scheme = [url scheme];
jens@17
    56
        if( scheme == nil )
jens@17
    57
            return nil;
jens@17
    58
    }
jens@17
    59
    // Check that scheme is allowed:
jens@17
    60
    if( allowedSchemes && ![allowedSchemes containsObject: scheme] )
jens@17
    61
        return nil;
jens@17
    62
    return url;
jens@17
    63
}
jens@17
    64
jens@17
    65
@end
jens@17
    66
jens@17
    67
jens@17
    68
jens@17
    69
jens@7
    70
@implementation NSHTTPURLResponse (MYUtilities)
jens@7
    71
jens@7
    72
jens@7
    73
- (NSError*) HTTPError
jens@7
    74
{
jens@7
    75
    // HTTP status >= 300 is considered an error:
jens@7
    76
    int status = self.statusCode;
jens@7
    77
    if( status >= 300 ) {
snej@19
    78
        NSString *reason = [NSHTTPURLResponse localizedStringForStatusCode: status];
jens@7
    79
        NSDictionary *info = $dict({NSLocalizedFailureReasonErrorKey,reason});
jens@7
    80
        return [NSError errorWithDomain: MyHTTPErrorDomain code: status userInfo: info];
jens@7
    81
    } else
jens@7
    82
        return nil;
jens@7
    83
}
jens@7
    84
jens@7
    85
jens@7
    86
NSString* const MyHTTPErrorDomain = @"HTTP";
jens@7
    87
jens@7
    88
jens@7
    89
@end
jens@11
    90
jens@11
    91
jens@11
    92
/*
jens@11
    93
 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
jens@11
    94
 
jens@11
    95
 Redistribution and use in source and binary forms, with or without modification, are permitted
jens@11
    96
 provided that the following conditions are met:
jens@11
    97
 
jens@11
    98
 * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@11
    99
 and the following disclaimer.
jens@11
   100
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
jens@11
   101
 and the following disclaimer in the documentation and/or other materials provided with the
jens@11
   102
 distribution.
jens@11
   103
 
jens@11
   104
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@11
   105
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@11
   106
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@11
   107
 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@11
   108
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@11
   109
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@11
   110
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@11
   111
 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@11
   112
 */