[tool] Fix IP parsing by using Uri constructor (#178083)

Also DRY'd up use of `"any"` constant is several places.

Fixes https://github.com/flutter/flutter/issues/178082
This commit is contained in:
Kevin Moore 2025-11-06 13:01:18 -08:00 committed by GitHub
parent 31a8481cd1
commit e979e166d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 12 deletions

View File

@ -207,8 +207,8 @@ class WebAssetServer implements AssetReader {
if (ddcModuleSystem) {
assert(canaryFeatures);
}
InternetAddress address;
if (hostname == 'any') {
final InternetAddress address;
if (hostname == webDevAnyHostDefault) {
address = InternetAddress.anyIPv4;
} else {
address = (await InternetAddress.lookup(hostname)).first;
@ -258,14 +258,15 @@ class WebAssetServer implements AssetReader {
fileSystem: fileSystem,
);
final int selectedPort = server.selectedPort;
var url = '$hostname:$selectedPort';
if (hostname == 'any') {
url = 'localhost:$selectedPort';
}
server._baseUri = Uri.http(url, server.basePath);
if (tlsCertPath != null && tlsCertKeyPath != null) {
server._baseUri = Uri.https(url, server.basePath);
}
final cleanHost = hostname == webDevAnyHostDefault ? 'localhost' : hostname;
final scheme = tlsCertPath != null && tlsCertKeyPath != null ? 'https' : 'http';
server._baseUri = Uri(
scheme: scheme,
host: cleanHost,
port: selectedPort,
path: server.basePath,
);
if (testMode) {
return server;
}

View File

@ -14,6 +14,11 @@ import '../base/os.dart';
import 'devfs_proxy.dart';
const webDevServerConfigFilePath = 'web_dev_config.yaml';
/// Represents the default value for the web dev server.
///
/// Maps to `localhost` and/or `127.0.0.1`.
const webDevAnyHostDefault = 'any';
const _kLogEntryPrefix = '[WebDevServer]';
const _kServer = 'server';
const _kName = 'name';
@ -41,7 +46,7 @@ T? _validateType<T>({required Object? value, required String fieldName}) {
class WebDevServerConfig {
const WebDevServerConfig({
this.headers = const <String, String>{},
this.host = 'any',
this.host = webDevAnyHostDefault,
this.port = 0,
this.https,
this.proxy = const <ProxyRule>[],
@ -97,7 +102,7 @@ class WebDevServerConfig {
return WebDevServerConfig(
headers: headers,
host: host ?? 'any',
host: host ?? webDevAnyHostDefault,
port: port ?? 0,
https: https == null ? null : HttpsConfig.fromYaml(https),
proxy: proxyRules,

View File

@ -1308,6 +1308,7 @@ void main() {
final String dummyCertPath = globals.fs.path.join(dataPath, 'tls_cert', 'dummy-cert.pem');
final String dummyCertKeyPath = globals.fs.path.join(dataPath, 'tls_cert', 'dummy-key.pem');
final webDevServerConfig = WebDevServerConfig(
host: '::1',
https: HttpsConfig(certPath: dummyCertPath, certKeyPath: dummyCertKeyPath),
);
final webDevFS = WebDevFS(
@ -1341,6 +1342,8 @@ void main() {
// Ensure the connection established is secure
expect(uri.scheme, 'https');
// Ensure that the host correctly support IPv6
expect(uri.host, '::1');
await webDevFS.destroy();
}, overrides: <Type, Generator>{Artifacts: () => Artifacts.test()}),