From 571e646543a2c1d20518399f62de37d76fe1be28 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Thu, 7 Jan 2016 04:00:15 -0800 Subject: [PATCH] Improved null url handling Summary: public Attempting to load an undefined URL via XMLHttpRequest produced a confusing error deep within the network layer. This diff improves the networking stack to catch such errors earlier, and also adds a helpful error in the JS layer. Fixes https://github.com/facebook/react-native/issues/4558 Reviewed By: javache Differential Revision: D2811080 fb-gh-sync-id: 1837427e1080a0308f2c4f9a8a42bce2e041fb48 --- Libraries/Network/RCTNetworking.m | 4 ++++ Libraries/Network/XMLHttpRequestBase.js | 3 +++ React/Base/RCTUtils.m | 6 +++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Libraries/Network/RCTNetworking.m b/Libraries/Network/RCTNetworking.m index 0c5aeaecd..6bbea21b1 100644 --- a/Libraries/Network/RCTNetworking.m +++ b/Libraries/Network/RCTNetworking.m @@ -136,6 +136,10 @@ RCT_EXPORT_MODULE() - (id)handlerForRequest:(NSURLRequest *)request { + if (!request.URL) { + return nil; + } + if (!_handlers) { // get handlers diff --git a/Libraries/Network/XMLHttpRequestBase.js b/Libraries/Network/XMLHttpRequestBase.js index 6c7bb8b48..4cdc42472 100644 --- a/Libraries/Network/XMLHttpRequestBase.js +++ b/Libraries/Network/XMLHttpRequestBase.js @@ -179,6 +179,9 @@ class XMLHttpRequestBase { // async is default throw new Error('Synchronous http requests are not supported'); } + if (!url) { + throw new Error('Cannot load an empty url'); + } this._reset(); this._method = method; this._url = url; diff --git a/React/Base/RCTUtils.m b/React/Base/RCTUtils.m index 48e1fea08..e6ebdb806 100644 --- a/React/Base/RCTUtils.m +++ b/React/Base/RCTUtils.m @@ -494,7 +494,11 @@ NSString *RCTBundlePathForURL(NSURL *URL) // Not a bundle-relative file return nil; } - return [path substringFromIndex:bundlePath.length + 1]; + path = [path substringFromIndex:bundlePath.length]; + if ([path hasPrefix:@"/"]) { + path = [path substringFromIndex:1]; + } + return path; } BOOL RCTIsXCAssetURL(NSURL *imageURL)