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 ) {
|
jens@7
|
78 |
NSString *reason = NSLocalizedStringFromTable( @"HTTP_ERROR_MESSAGE",@"UKCrashReporter",@"");
|
jens@7
|
79 |
reason = [NSHTTPURLResponse localizedStringForStatusCode: status];
|
jens@7
|
80 |
NSDictionary *info = $dict({NSLocalizedFailureReasonErrorKey,reason});
|
jens@7
|
81 |
return [NSError errorWithDomain: MyHTTPErrorDomain code: status userInfo: info];
|
jens@7
|
82 |
} else
|
jens@7
|
83 |
return nil;
|
jens@7
|
84 |
}
|
jens@7
|
85 |
|
jens@7
|
86 |
|
jens@7
|
87 |
NSString* const MyHTTPErrorDomain = @"HTTP";
|
jens@7
|
88 |
|
jens@7
|
89 |
|
jens@7
|
90 |
@end
|
jens@11
|
91 |
|
jens@11
|
92 |
|
jens@11
|
93 |
/*
|
jens@11
|
94 |
Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
|
jens@11
|
95 |
|
jens@11
|
96 |
Redistribution and use in source and binary forms, with or without modification, are permitted
|
jens@11
|
97 |
provided that the following conditions are met:
|
jens@11
|
98 |
|
jens@11
|
99 |
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
jens@11
|
100 |
and the following disclaimer.
|
jens@11
|
101 |
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
jens@11
|
102 |
and the following disclaimer in the documentation and/or other materials provided with the
|
jens@11
|
103 |
distribution.
|
jens@11
|
104 |
|
jens@11
|
105 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
jens@11
|
106 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
jens@11
|
107 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
|
jens@11
|
108 |
BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
jens@11
|
109 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
jens@11
|
110 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
jens@11
|
111 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
jens@11
|
112 |
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
jens@11
|
113 |
*/
|