forked from yannisroy/MYUtilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMYURLUtils.m
187 lines (154 loc) · 7.08 KB
/
MYURLUtils.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// MYURLUtils.m
// MYUtilities
//
// Created by Jens Alfke on 5/15/12.
// Copyright (c) 2012 Jens Alfke. All rights reserved.
//
#import "MYURLUtils.h"
#import "Test.h"
@implementation NSURL (MYUtilities)
- (UInt16) my_effectivePort {
TestedBy(MYURLUtils);
NSNumber* portObj = self.port;
if (Cover(portObj))
return portObj.unsignedShortValue;
return self.my_isHTTPS ? 443 : 80;
}
- (BOOL) my_isHTTPS {
return (0 == [self.scheme caseInsensitiveCompare: @"https"]);
}
- (NSURL*) my_baseURL {
TestedBy(MYURLUtils);
NSString* scheme = self.scheme.lowercaseString;
NSMutableString* str = [NSMutableString stringWithFormat: @"%@://%@",
scheme, self.host.lowercaseString];
NSNumber* port = self.port;
if (Cover(port)) {
int defaultPort = [scheme isEqualToString: @"https"] ? 443 : 80;
if (Cover(port.intValue != defaultPort))
[str appendFormat: @":%@", port];
}
return [NSURL URLWithString: str];
}
- (NSString*) my_pathAndQuery {
CFStringRef path = CFURLCopyPath((CFURLRef)self);
CFStringRef resource = CFURLCopyResourceSpecifier((CFURLRef)self);
NSString* result = [(id)path stringByAppendingString: (id)resource];
CFRelease(path);
CFRelease(resource);
return result;
}
- (NSURL*) my_URLByRemovingUser {
TestedBy(MYURLUtils);
CFRange userRange, userPlusDelimRange, passPlusDelimRange;
userRange = CFURLGetByteRangeForComponent((CFURLRef)self, kCFURLComponentUser, &userPlusDelimRange);
CFURLGetByteRangeForComponent((CFURLRef)self, kCFURLComponentPassword, &passPlusDelimRange);
if (Cover(userRange.length == 0))
return self;
CFIndex delEnd;
if (Cover(passPlusDelimRange.length == 0))
delEnd = userPlusDelimRange.location + userPlusDelimRange.length;
else
delEnd = passPlusDelimRange.location+passPlusDelimRange.length;
UInt8 urlBytes[1024];
CFIndex nBytes = CFURLGetBytes((CFURLRef)self, urlBytes, sizeof(urlBytes) - 1);
if (nBytes < 0)
return self;
memmove(urlBytes + userRange.location,
urlBytes + delEnd,
nBytes - delEnd);
nBytes -= delEnd - userRange.location;
CFURLRef newURL = CFURLCreateWithBytes(NULL, urlBytes, nBytes,
kCFStringEncodingUTF8, NULL);
Assert(newURL != nil);
return [(id)newURL autorelease];
}
- (NSURLProtectionSpace*) my_protectionSpaceWithRealm: (NSString*)realm
authenticationMethod: (NSString*)authenticationMethod
{
NSString* protocol = self.my_isHTTPS ? NSURLProtectionSpaceHTTPS
: NSURLProtectionSpaceHTTP;
return [[[NSURLProtectionSpace alloc] initWithHost: self.host
port: self.my_effectivePort
protocol: protocol
realm: realm
authenticationMethod: authenticationMethod]
autorelease];
}
- (NSURLCredential*) my_credentialForRealm: (NSString*)realm
authenticationMethod: (NSString*)authenticationMethod
{
if ($equal(authenticationMethod, NSURLAuthenticationMethodServerTrust))
return nil;
NSString* username = self.user;
NSString* password = self.password;
if (username && password)
return [NSURLCredential credentialWithUser: username password: password
persistence: NSURLCredentialPersistenceForSession];
NSURLProtectionSpace* space = [self my_protectionSpaceWithRealm: realm
authenticationMethod: authenticationMethod];
NSURLCredentialStorage* storage = [NSURLCredentialStorage sharedCredentialStorage];
if (username)
return [[storage credentialsForProtectionSpace: space] objectForKey: username];
else
return [storage defaultCredentialForProtectionSpace: space];
}
- (NSDictionary*) my_proxySettings {
CFDictionaryRef proxySettings = CFNetworkCopySystemProxySettings();
if (!proxySettings)
return nil;
NSArray* proxies = CFBridgingRelease(CFNetworkCopyProxiesForURL((__bridge CFURLRef)self,
proxySettings));
CFRelease(proxySettings);
if (proxies.count == 0)
return nil;
NSDictionary* proxy = proxies[0];
if ($equal(proxy[(id)kCFProxyTypeKey], (id)kCFProxyTypeNone))
return nil;
return proxy;
}
- (NSString*) my_sanitizedString {
TestedBy(MYURLUtils);
CFRange passRange = CFURLGetByteRangeForComponent((CFURLRef)self, kCFURLComponentPassword, NULL);
if (Cover(passRange.length == 0))
return self.absoluteString;
NSUInteger passEnd = passRange.location + passRange.length;
CFIndex nBytes = CFURLGetBytes((CFURLRef)self, NULL, 0);
UInt8 urlBytes[nBytes];
CFURLGetBytes((CFURLRef)self, urlBytes, sizeof(urlBytes));
NSString* before = [[NSString alloc] initWithBytes: urlBytes
length: passRange.location
encoding:NSUTF8StringEncoding];
NSString* after = [[NSString alloc] initWithBytes: &urlBytes[passEnd]
length: (nBytes - passEnd)
encoding:NSUTF8StringEncoding];
NSString* result = [NSString stringWithFormat: @"%@*****%@", before, after];
[before release];
[after release];
return result;
}
@end
TestCase(MYURLUtils) {
NSURL* url = $url(@"https://example.com/path/here?query#fragment");
CAssertEq(url.my_effectivePort, 443);
CAssertEqual(url.my_baseURL, $url(@"https://example.com"));
CAssertEqual(url.my_URLByRemovingUser, url);
CAssertEqual(url.my_sanitizedString, @"https://example.com/path/here?query#fragment");
url = $url(@"https://example.com:8080/path/here?query#fragment");
CAssertEq(url.my_effectivePort, 8080);
CAssertEqual(url.my_baseURL, $url(@"https://example.com:8080"));
CAssertEqual(url.my_URLByRemovingUser, url);
CAssertEqual(url.my_sanitizedString, @"https://example.com:8080/path/here?query#fragment");
CAssertEqual($url(@"http://example.com:80/path/here?query#fragment").my_baseURL,
$url(@"http://example.com"));
CAssertEq($url(@"http://example.com:80/path/here?query#fragment").my_effectivePort, 80);
CAssertEqual($url(@"https://example.com:443/path/here?query#fragment").my_baseURL,
$url(@"https://example.com"));
url = $url(@"https://[email protected]/path/here?query#fragment");
CAssertEqual(url.my_URLByRemovingUser, $url(@"https://example.com/path/here?query#fragment"));
CAssertEqual(url.my_sanitizedString, @"https://[email protected]/path/here?query#fragment");
url = $url(@"https://bob:[email protected]/path/here?query#fragment");
CAssertEqual(url.my_URLByRemovingUser, $url(@"https://example.com/path/here?query#fragment"));
CAssertEqual(url.my_sanitizedString, @"https://bob:*****@example.com/path/here?query#fragment");
}