Skip to content

Instantly share code, notes, and snippets.

@boredzo
Last active December 29, 2015 15:08
Show Gist options
  • Save boredzo/7688181 to your computer and use it in GitHub Desktop.
Save boredzo/7688181 to your computer and use it in GitHub Desktop.
Test app for detecting URLs with no scheme
#import <Foundation/Foundation.h>
static void test(NSString *str);
int main(int argc, char *argv[]) {
@autoreleasepool {
test(@"mailto:[email protected]");
test(@"mailto:[email protected]?subject=Support%20Request&body=I%20NEED%20HELP%21");
test(@"callto://examplesteve");
test(@"callto:+17145551234");
test(@"skype://examplesteve");
test(@"skype:examplesteve");
test(@"facetime:[email protected]");
test(@"example.com");
test(@"example.com/");
test(@"example.com:8080");
test(@"example.com:8080/");
test(@"example.com/pathname/39150189");
test(@"example.com/pathname/39150189?x=y&a=b");
test(@"example.com/pathname/39150189#things");
test(@"example.com/pathname/39150189#!ajax");
test(@"example.com/pathname/39150189#!ajax=true&fancy=snowman");
test(@"example.com/pathname/39150189#!ajax=true&fancy=");
test(@"example.com/pathname/39150189#!ajax=true&fancy=☃");
test(@":example.com");
test(@":example.com/");
test(@":example.com:8080");
test(@":example.com:8080/");
test(@":example.com/pathname/39150189");
test(@":example.com/pathname/39150189?x=y&a=b");
test(@":example.com/pathname/39150189#things");
test(@":example.com/pathname/39150189#!ajax");
test(@":example.com/pathname/39150189#!ajax=true&fancy=snowman");
test(@":example.com/pathname/39150189#!ajax=true&fancy=");
test(@":example.com/pathname/39150189#!ajax=true&fancy=☃");
test(@"://example.com");
test(@"://example.com/");
test(@"://example.com:8080");
test(@"://example.com:8080/");
test(@"://example.com/pathname/39150189");
test(@"://example.com/pathname/39150189?x=y&a=b");
test(@"://example.com/pathname/39150189#things");
test(@"://example.com/pathname/39150189#!ajax");
test(@"://example.com/pathname/39150189#!ajax=true&fancy=snowman");
test(@"://example.com/pathname/39150189#!ajax=true&fancy=");
test(@"://example.com/pathname/39150189#!ajax=true&fancy=☃");
}
return EXIT_SUCCESS;
}
NSString *PRHPrefixURLStringWithHTTPButOnlyIfAppropriate(NSString *str) {
NSString *modified = str;
modified = [modified stringByReplacingOccurrencesOfString:@"://"
withString:@"http://"
options:NSAnchoredSearch
range:(NSRange){ 0, str.length }
];
modified = [modified stringByReplacingOccurrencesOfString:@":"
withString:@"http://"
options:NSAnchoredSearch
range:(NSRange){ 0, str.length }
];
NSURL *URL = [NSURL URLWithString:modified];
if ((URL != nil) && (((URL.scheme == nil) || (URL.path == nil)) && ! ([URL.scheme isEqualToString:@"mailto"] || [URL.scheme isEqualToString:@"callto"] || [URL.scheme isEqualToString:@"skype"] || [URL.scheme isEqualToString:@"facetime"]))) {
modified = [@"http://" stringByAppendingString:str];
}
return modified;
}
static void test(NSString *str) {
NSString *modified = PRHPrefixURLStringWithHTTPButOnlyIfAppropriate(str);
NSURL *URL = [NSURL URLWithString:modified];
NSLog(@"%p! %@-> %@ :// %@ : %@ @ %@ :%@ %@ ?%@\n-> %@", URL, str, URL.scheme, URL.user, URL.password, URL.host, URL.port, URL.path, URL.query, modified);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment