Skip to content

Commit

Permalink
Validate the content type of the bundle returned by the packager
Browse files Browse the repository at this point in the history
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.

<img width="354" alt="screen shot 2017-07-19 at 8 01 58 pm" src="https://user-images.githubusercontent.com/2677334/28394905-39e86d52-6cbe-11e7-9059-13a85816a57e.png">
Closes facebook#15112

Differential Revision: D5459395

Pulled By: shergin

fbshipit-source-id: aaea7ab2e1311ee8dc10feb579adf9b9701d8d4c
  • Loading branch information
janicduplessis authored and ide committed Aug 2, 2017
1 parent 5f23923 commit 57d8f61
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions React/Base/RCTJavaScriptLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}];

Expand Down

0 comments on commit 57d8f61

Please sign in to comment.