validateAndNormalizeHostedUrl function
Validates and normalizes a hostedUrl which is pointing to a pub server.
A hostedUrl is a URL pointing to a hosted pub server as defined by the
repository-spec-v2. The default value is pub.flutter-io.cn, and can be
overwritten using PUB_HOSTED_URL. It can also specified for individual
hosted-dependencies in pubspec.yaml, and for the root package using the
publish_to key.
The hostedUrl is always normalized to a Uri with path that ends in slash
unless the path is merely /, in which case we normalize to the bare domain
this keeps the hostedUrl and maintains avoids unnecessary churn in
pubspec.lock files which contain https://pub.flutter-io.cn.
Throws FormatException if there is anything wrong hostedUrl.
Implementation
Uri validateAndNormalizeHostedUrl(String hostedUrl) {
Uri u;
try {
u = Uri.parse(hostedUrl);
} on FormatException catch (e) {
throw FormatException(
'invalid url: ${e.message}',
e.source,
e.offset,
);
}
if (!u.hasScheme || (u.scheme != 'http' && u.scheme != 'https')) {
throw FormatException('url scheme must be https:// or http://', hostedUrl);
}
if (!u.hasAuthority || u.host == '') {
throw FormatException('url must have a hostname', hostedUrl);
}
if (u.userInfo != '') {
throw FormatException('user-info is not supported in url', hostedUrl);
}
if (u.hasQuery) {
throw FormatException('querystring is not supported in url', hostedUrl);
}
if (u.hasFragment) {
throw FormatException('fragment is not supported in url', hostedUrl);
}
u = u.normalizePath();
// If we have a path of only `/`
if (u.path == '/') {
u = u.replace(path: '');
}
// If there is a path, and it doesn't end in a slash we normalize to slash
if (u.path.isNotEmpty && !u.path.endsWith('/')) {
u = u.replace(path: '${u.path}/');
}
return u;
}