From 3ce36698de9d2afac5cdaf6250287884defcc6c0 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Wed, 19 Jul 2017 18:42:49 -0700 Subject: [PATCH] Validate the content type of the bundle returned by the packager Summary: When using the packager behind something like an http tunnel it is possible something else than JS is returned, in that case throw an error instead of trying to parse it. This is useful for Expo since the packager runs behind ngrok. This allows to intercept that error and show a more meaningful error message based on the ngrok response. **Test plan** Tested by changing the packager to return text/html content type and validate that the error shows up properly. Also tested that it works when multipart response is disabled. screen shot 2017-07-19 at 8 01 58 pm Closes https://github.com/facebook/react-native/pull/15112 Differential Revision: D5459395 Pulled By: shergin fbshipit-source-id: aaea7ab2e1311ee8dc10feb579adf9b9701d8d4c --- React/Base/RCTJavaScriptLoader.mm | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/React/Base/RCTJavaScriptLoader.mm b/React/Base/RCTJavaScriptLoader.mm index eb7c11e51..f2598ad66 100755 --- a/React/Base/RCTJavaScriptLoader.mm +++ b/React/Base/RCTJavaScriptLoader.mm @@ -243,6 +243,21 @@ static void attemptAsynchronousLoadOfBundleAtURL(NSURL *scriptURL, RCTSourceLoad onComplete(error, nil, 0); return; } + + // Validate that the packager actually returned javascript. + NSString *contentType = headers[@"Content-Type"]; + if (![contentType isEqualToString:@"application/javascript"]) { + error = [NSError errorWithDomain:@"JSServer" + code:NSURLErrorCannotParseResponse + userInfo:@{ + NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Expected JavaScript, but got content type '%@'.", contentType], + @"headers": headers, + @"data": data + }]; + onComplete(error, nil, 0); + return; + } + onComplete(nil, data, data.length); }];