Permalink
Browse files

Merge branch 'sandvox-2.7'

  • Loading branch information...
2 parents 6ca2ff0 + 6c1ea7a commit 34e4e8f6b816faba6bcc25e6b8c8e262b4990fa6 @mikeabdullah mikeabdullah committed Oct 5, 2012
Showing with 37 additions and 0 deletions.
  1. +1 −0 KSURLUtilities.h
  2. +30 −0 KSURLUtilities.m
  3. +6 −0 TestKSFileUtilities/TestKSURLUtilities.m
View
@@ -37,6 +37,7 @@
- (NSURL *)ks_hostURL;
- (NSArray *)ks_domains;
- (BOOL)ks_hasNetworkLocation; // checks for a host with 2+ domains
+- (NSURL *)ks_URLWithHost:(NSString *)host; // swaps out host for a new one
#pragma mark Paths
View
@@ -76,6 +76,36 @@ - (BOOL)ks_hasNetworkLocation
return result;
}
+- (NSURL *)ks_URLWithHost:(NSString *)host;
+{
+ NSParameterAssert(host);
+
+ NSURL *result = nil;
+ CFURLRef absolute = CFURLCopyAbsoluteURL((CFURLRef)self);
+ CFRange range = CFURLGetByteRangeForComponent(absolute, kCFURLComponentHost, NULL);
+
+ if (range.location != kCFNotFound)
+ {
+ // Grab data
+ CFIndex length = CFURLGetBytes(absolute, NULL, 0);
+ NSMutableData *data = [[NSMutableData alloc] initWithLength:length];
+ length = CFURLGetBytes(absolute, [data mutableBytes], [data length]);
+ NSAssert(length == [data length], @"CFURLGetBytes() lied to us!");
+
+ // Replace the host
+ NSData *hostData = [host dataUsingEncoding:NSASCIIStringEncoding];
+ [data replaceBytesInRange:NSMakeRange(range.location, range.length) withBytes:[hostData bytes] length:[hostData length]];
+
+ // Create final URL
+ result = NSMakeCollectable(CFURLCreateWithBytes(NULL, [data bytes], [data length], kCFStringEncodingASCII, NULL));
+ [result autorelease];
+ [data release];
+ }
+
+ CFRelease(absolute);
+ return result;
+}
+
#pragma mark Paths
/* These two methods operate very similarly to -initWithString:relativeToURL:
@@ -147,6 +147,12 @@ - (void)testURLRelativeToURL
nil);
}
+- (void)testHostSwapping;
+{
+ STAssertEqualObjects([URL(@"http://example.com") ks_URLWithHost:@"test.net"], URL(@"http://test.net"), nil);
+ STAssertEqualObjects([URL(@"http://example.com/") ks_URLWithHost:@"test.net"], URL(@"http://test.net/"), nil);
+}
+
- (void)testURLHasDirectoryPath;
{
STAssertFalse([[NSURL URLWithString:@"http://example.com/foo"] ks_hasDirectoryPath], @"No trailing slash");

0 comments on commit 34e4e8f

Please sign in to comment.