Commit e340c199 authored by Jonah Williams's avatar Jonah Williams Committed by Flutter GitHub Bot

[flutter_tools] resolve host address in the flutter tool for web server (#48992)

parent da0bfd1c
......@@ -402,7 +402,7 @@ class _ExperimentalResidentWebRunner extends ResidentWebRunner {
hostPort,
packagesFilePath,
);
await device.devFS.create();
final Uri url = await device.devFS.create();
await _updateDevFS(fullRestart: true);
device.generator.accept();
await device.device.startApp(
......@@ -410,7 +410,7 @@ class _ExperimentalResidentWebRunner extends ResidentWebRunner {
mainPath: target,
debuggingOptions: debuggingOptions,
platformArgs: <String, Object>{
'uri': 'http://$effectiveHostname:$hostPort',
'uri': url.toString(),
},
);
return attach(
......
......@@ -333,13 +333,17 @@ class WebFs {
Cascade cascade = Cascade();
cascade = cascade.add(handler);
cascade = cascade.add(assetServer.handle);
final HttpServer server = await httpMultiServerFactory(effectiveHostname, hostPort);
final InternetAddress internetAddress = (await InternetAddress.lookup(effectiveHostname)).first;
final HttpServer server = await httpMultiServerFactory(internetAddress, hostPort);
shelf_io.serveRequests(server, cascade.handler);
final WebFs webFS = WebFs(
client,
server,
dwds,
'http://$effectiveHostname:$hostPort/',
// Format ipv6 hosts according to RFC 5952.
internetAddress.type == InternetAddressType.IPv4
? '${internetAddress.address}:$hostPort'
: '[${internetAddress.address}]:$hostPort',
assetServer,
buildInfo.isDebug,
flutterProject,
......
......@@ -28,7 +28,7 @@ import 'bootstrap.dart';
/// This is only used in development mode.
class WebAssetServer {
@visibleForTesting
WebAssetServer(this._httpServer, this._packages,
WebAssetServer(this._httpServer, this._packages, this.internetAddress,
{@required void Function(dynamic, StackTrace) onError}) {
_httpServer.listen((HttpRequest request) {
_handleRequest(request).catchError(onError);
......@@ -46,10 +46,11 @@ class WebAssetServer {
/// trace.
static Future<WebAssetServer> start(String hostname, int port) async {
try {
final HttpServer httpServer = await HttpServer.bind(hostname, port);
final InternetAddress address = (await InternetAddress.lookup(hostname)).first;
final HttpServer httpServer = await HttpServer.bind(address, port);
final Packages packages =
await loadPackagesFile(Uri.base.resolve('.packages'));
return WebAssetServer(httpServer, packages,
return WebAssetServer(httpServer, packages, address,
onError: (dynamic error, StackTrace stackTrace) {
httpServer.close(force: true);
throwToolExit(
......@@ -71,6 +72,7 @@ class WebAssetServer {
final RegExp _drivePath = RegExp(r'\/[A-Z]:\/');
final Packages _packages;
final InternetAddress internetAddress;
// handle requests for JavaScript source, dart sources maps, or asset files.
Future<void> _handleRequest(HttpRequest request) async {
......@@ -274,7 +276,13 @@ class WebDevFS implements DevFS {
@override
Future<Uri> create() async {
_webAssetServer = await WebAssetServer.start(hostname, port);
return Uri.base;
final InternetAddress internetAddress = _webAssetServer.internetAddress;
// Format ipv6 hosts according to RFC 5952.
return Uri.parse(
internetAddress.type == InternetAddressType.IPv4
? '${internetAddress.address}:$port'
: '[${internetAddress.address}]:$port'
);
}
@override
......
......@@ -68,7 +68,7 @@ void main() {
closeCompleter.complete();
});
webAssetServer = WebAssetServer(
mockHttpServer, packages, onError: (dynamic error, StackTrace stackTrace) {
mockHttpServer, packages, InternetAddress.loopbackIPv4, onError: (dynamic error, StackTrace stackTrace) {
closeCompleter.completeError(error, stackTrace);
});
});
......
......@@ -32,11 +32,9 @@ void main() {
MockOperatingSystemUtils mockOperatingSystemUtils;
MockProcessUtils mockProcessUtils;
bool lastInitializePlatform;
dynamic lastAddress;
int lastPort;
setUp(() {
lastAddress = null;
lastPort = null;
lastInitializePlatform = null;
mockBuildDaemonCreator = MockBuildDaemonCreator();
......@@ -95,7 +93,6 @@ void main() {
ChromeLauncher: () => mockChromeLauncher,
ProcessUtils: () => mockProcessUtils,
HttpMultiServerFactory: () => (dynamic address, int port) async {
lastAddress = address;
lastPort = port;
return mockHttpMultiServer;
},
......@@ -178,15 +175,17 @@ void main() {
buildInfo: BuildInfo.debug,
flutterProject: flutterProject,
initializePlatform: false,
hostname: 'foo',
hostname: 'localhost',
port: '1234',
urlTunneller: null,
dartDefines: const <String>[],
);
expect(webFs.uri, contains('foo:1234'));
// Might be either ipv4 or ipv6 for localhost.
final bool hasExpectedUri = webFs.uri.toString().contains('[::1]:1234') ||
webFs.uri.toString().contains('127.0.0.1:1234');
expect(hasExpectedUri, true);
expect(lastPort, 1234);
expect(lastAddress, contains('foo'));
}));
test('Throws exception if build fails', () => testbed.run(() async {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment