jens@7: // jens@7: // URLUtils.m jens@11: // MYUtilities jens@7: // jens@7: // Created by Jens Alfke on 4/28/08. jens@11: // Copyright 2008 Jens Alfke. All rights reserved. jens@7: // jens@7: jens@7: #import "URLUtils.h" jens@7: jens@7: jens@17: @implementation NSURL (MYUtilities) jens@17: jens@17: + (NSString*) my_stringByTrimmingURLString: (NSString*)string jens@17: { jens@17: NSMutableString *trimmed = [[string mutableCopy] autorelease]; jens@17: NSRange r; jens@17: // Remove all whitespace and newlines: jens@17: while(YES){ jens@17: r = [trimmed rangeOfCharacterFromSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; jens@17: if( r.length==0 ) jens@17: break; jens@17: [trimmed replaceCharactersInRange: r withString: @""]; jens@17: } jens@17: jens@17: // Delete surrounding "<...>": jens@17: r = NSMakeRange(0,trimmed.length); jens@17: if( [trimmed hasPrefix: @"<"] ) { jens@17: r.location++; jens@17: r.length--; jens@17: } jens@17: if( [trimmed hasSuffix: @">"] ) jens@17: r.length--; jens@17: return [trimmed substringWithRange: r]; jens@17: } jens@17: jens@17: + (NSURL*) my_URLWithLenientString: (NSString*)string jens@17: defaultScheme: (NSString*)defaultScheme jens@17: allowedSchemes: (NSArray*)allowedSchemes jens@17: { jens@17: // Trim it: jens@17: string = [self my_stringByTrimmingURLString: string]; jens@17: if( string.length==0 ) jens@17: return nil; jens@17: NSURL *url = [NSURL URLWithString: string]; jens@17: if( ! url ) jens@17: return nil; jens@17: // Apply default scheme (if any): jens@17: NSString *scheme = url.scheme.lowercaseString; jens@17: if( scheme == nil ) { jens@17: if( ! defaultScheme ) jens@17: return nil; jens@17: string = $sprintf(@"%@://%@", defaultScheme,string); jens@17: url = [NSURL URLWithString: string]; jens@17: scheme = [url scheme]; jens@17: if( scheme == nil ) jens@17: return nil; jens@17: } jens@17: // Check that scheme is allowed: jens@17: if( allowedSchemes && ![allowedSchemes containsObject: scheme] ) jens@17: return nil; jens@17: return url; jens@17: } jens@17: jens@17: @end jens@17: jens@17: jens@17: jens@17: jens@7: @implementation NSHTTPURLResponse (MYUtilities) jens@7: jens@7: jens@7: - (NSError*) HTTPError jens@7: { jens@7: // HTTP status >= 300 is considered an error: jens@7: int status = self.statusCode; jens@7: if( status >= 300 ) { snej@19: NSString *reason = [NSHTTPURLResponse localizedStringForStatusCode: status]; jens@7: NSDictionary *info = $dict({NSLocalizedFailureReasonErrorKey,reason}); jens@7: return [NSError errorWithDomain: MyHTTPErrorDomain code: status userInfo: info]; jens@7: } else jens@7: return nil; jens@7: } jens@7: jens@7: jens@7: NSString* const MyHTTPErrorDomain = @"HTTP"; jens@7: jens@7: jens@7: @end jens@11: jens@11: jens@11: /* jens@11: Copyright (c) 2008, Jens Alfke . All rights reserved. jens@11: jens@11: Redistribution and use in source and binary forms, with or without modification, are permitted jens@11: provided that the following conditions are met: jens@11: jens@11: * Redistributions of source code must retain the above copyright notice, this list of conditions jens@11: and the following disclaimer. jens@11: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions jens@11: and the following disclaimer in the documentation and/or other materials provided with the jens@11: distribution. jens@11: jens@11: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR jens@11: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jens@11: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- jens@11: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES jens@11: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jens@11: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN jens@11: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF jens@11: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jens@11: */