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