From de7ea51fd47dfd4042fe026acdf28fe44dee3720 Mon Sep 17 00:00:00 2001 From: James Ide Date: Thu, 30 Apr 2015 15:01:13 -0700 Subject: [PATCH] Lowercase the supported URL schemes, and use scheme list to determine if URL is internal If a URL has no scheme, it is considered an internal URL. If it has a scheme, we need to lop off the trailing ":" from the protocol and then compare it against the entries in the supported scheme list. Note that Cocoa does not automatically lowercase the supported schemes (we do this in native though) and Node's url library does. --- URLHandler.js | 19 +++++++++++++++---- iOS/NTURLHandler/NTURLHandler.m | 4 +++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/URLHandler.js b/URLHandler.js index 6b81f29..50eacab 100644 --- a/URLHandler.js +++ b/URLHandler.js @@ -46,10 +46,8 @@ var URLHandler = { * without a host are treated as in-app URLs. */ openURL(targetURL: string) { - // Parse the query string and have "//" denote the hostname - var components = url.parse(targetURL, false, true); - if (components.protocol === 'xxx:') { - emitter.emit('request', url); + if (this.isInternalURL(targetURL)) { + emitter.emit('request', targetURL); } else { NTURLHandler.openURL(targetURL, () => {}, (error) => { console.error('Error opening URL: ' + error.stack); @@ -57,6 +55,19 @@ var URLHandler = { } }, + /** + * Returns whether the given URL is an in-app URL or an external URL. + */ + isInternalURL(targetURL: string): bool { + // Parse the query string and have "//" denote the hostname + var {protocol} = url.parse(targetURL, false, true); + if (!protocol) { + return true; + } + var scheme = protocol.substring(0, protocol.length - 1); + return this.schemes.indexOf(scheme) !== -1; + }, + /** * Adds a listener that receives an object with URL information when the app * has been instructed to open a URL. See the Request type. diff --git a/iOS/NTURLHandler/NTURLHandler.m b/iOS/NTURLHandler/NTURLHandler.m index d12244c..e23daa0 100644 --- a/iOS/NTURLHandler/NTURLHandler.m +++ b/iOS/NTURLHandler/NTURLHandler.m @@ -74,7 +74,9 @@ static NSString * const NTURLHandlerOpenURLNotification = @"NTURLHandlerOpenURL" NSMutableSet *schemes = [NSMutableSet set]; NSDictionary *info = [NSBundle mainBundle].infoDictionary; for (NSDictionary *urlType in info[@"CFBundleURLTypes"]) { - [schemes addObjectsFromArray:urlType[@"CFBundleURLSchemes"]]; + for (NSString *scheme in urlType[@"CFBundleURLSchemes"]) { + [schemes addObject:scheme.lowercaseString]; + } } return schemes; }