feat: 更新 H5 线路配置逻辑,添加默认线路处理和相关单元测试
This commit is contained in:
@@ -42,8 +42,7 @@ class AppConfig {
|
||||
|
||||
static List<H5Line> get h5Lines {
|
||||
final configuredUrls = _dartDefinedH5LineUrls.trim().isEmpty
|
||||
? _normalizedUniqueUrls(_environmentHosts[currentEnvironment] ??
|
||||
const [_bootstrapH5LineUrl])
|
||||
? _normalizedUniqueUrls(defaultH5LineUrlsForBaseUri(Uri.base))
|
||||
: _normalizedUniqueUrls(_dartDefinedH5LineUrls.split(','));
|
||||
final urls = configuredUrls.isEmpty
|
||||
? _normalizedUniqueUrls(const [_bootstrapH5LineUrl])
|
||||
@@ -71,18 +70,26 @@ class AppConfig {
|
||||
return const [];
|
||||
}
|
||||
|
||||
final homeUri = lines.first.uri;
|
||||
return clientConfigQueryUrisForHomeUri(lines.first.uri);
|
||||
}
|
||||
|
||||
static List<String> defaultH5LineUrlsForBaseUri(Uri baseUri) {
|
||||
final runtimeHomeUrl = _runtimeHomeUrlFromBaseUri(baseUri);
|
||||
if (runtimeHomeUrl != null) {
|
||||
return [runtimeHomeUrl];
|
||||
}
|
||||
|
||||
final environmentHosts = _environmentHosts[currentEnvironment];
|
||||
if (environmentHosts == null || environmentHosts.isEmpty) {
|
||||
return const [_bootstrapH5LineUrl];
|
||||
}
|
||||
return environmentHosts;
|
||||
}
|
||||
|
||||
static List<Uri> clientConfigQueryUrisForHomeUri(Uri homeUri) {
|
||||
return [
|
||||
homeUri.replace(
|
||||
path: '/client_config/query',
|
||||
queryParameters: const {},
|
||||
fragment: '',
|
||||
),
|
||||
homeUri.replace(
|
||||
path: '/api/user/client_config/query',
|
||||
queryParameters: const {},
|
||||
fragment: '',
|
||||
),
|
||||
_originUriWithPath(homeUri, '/client_config/query'),
|
||||
_originUriWithPath(homeUri, '/api/user/client_config/query'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -191,6 +198,69 @@ class AppConfig {
|
||||
return uri.replace(path: path).toString();
|
||||
}
|
||||
|
||||
static String? _runtimeHomeUrlFromBaseUri(Uri baseUri) {
|
||||
if (_isLocalRuntimeUri(baseUri)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return _originUriWithPath(baseUri, '/').toString();
|
||||
}
|
||||
|
||||
static Uri _originUriWithPath(Uri uri, String path) {
|
||||
final normalizedPath = path.startsWith('/') ? path : '/$path';
|
||||
return Uri.parse('${uri.scheme}://${uri.authority}$normalizedPath');
|
||||
}
|
||||
|
||||
static bool _isLocalRuntimeUri(Uri uri) {
|
||||
if (uri.scheme != 'http' && uri.scheme != 'https') {
|
||||
return true;
|
||||
}
|
||||
|
||||
final host = _normalizeHostForCompare(uri.host);
|
||||
if (host.isEmpty) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (host == 'localhost' || host.endsWith('.localhost') || host == '::1') {
|
||||
return true;
|
||||
}
|
||||
|
||||
final ipv4Parts = _tryParseIpv4Address(host);
|
||||
if (ipv4Parts == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ipv4Parts[0] == 127 || ipv4Parts.every((part) => part == 0);
|
||||
}
|
||||
|
||||
static String _normalizeHostForCompare(String host) {
|
||||
final normalized = host.trim().toLowerCase();
|
||||
if (normalized.endsWith('.')) {
|
||||
return normalized.substring(0, normalized.length - 1);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
static List<int>? _tryParseIpv4Address(String host) {
|
||||
final parts = host.split('.');
|
||||
if (parts.length != 4) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final octets = <int>[];
|
||||
for (final part in parts) {
|
||||
if (part.isEmpty || !RegExp(r'^\d+$').hasMatch(part)) {
|
||||
return null;
|
||||
}
|
||||
final value = int.tryParse(part);
|
||||
if (value == null || value < 0 || value > 255) {
|
||||
return null;
|
||||
}
|
||||
octets.add(value);
|
||||
}
|
||||
return octets;
|
||||
}
|
||||
|
||||
static String _replaceWildcardHost(
|
||||
String value, {
|
||||
String Function()? wildcardFactory,
|
||||
|
||||
Reference in New Issue
Block a user