fix: Provide a reasonable well-known fallback

If the well-known look fails (not json, 404, etc.) we should assume a
reasonable fallback (domain part with https prepended). As clients are
expected to call Client.checkHomeserver on the resulting domain anyways
we can safely assume this default, as it is still validated, if there
is actually a matrix homeserver running on that endpoint.
This commit is contained in:
Sorunome 2021-07-11 12:26:48 +02:00
parent 2980f6d8e3
commit e86353a412
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
1 changed files with 17 additions and 7 deletions

View File

@ -296,8 +296,9 @@ class Client extends MatrixApi {
Future<WellKnownInformation> getWellKnownInformationsByUserId(
String MatrixIdOrDomain,
) async {
final response = await http
.get(Uri.https(MatrixIdOrDomain.domain, '/.well-known/matrix/client'));
try {
final response = await http.get(
Uri.https(MatrixIdOrDomain.domain, '/.well-known/matrix/client'));
var respBody = response.body;
try {
respBody = utf8.decode(response.bodyBytes);
@ -306,6 +307,15 @@ class Client extends MatrixApi {
}
final rawJson = json.decode(respBody);
return WellKnownInformation.fromJson(rawJson);
} catch (_) {
// we got an error processing or fetching the well-known information, let's
// provide a reasonable fallback.
return WellKnownInformation.fromJson(<String, dynamic>{
'm.homeserver': <String, dynamic>{
'base_url': Uri.https(MatrixIdOrDomain.domain, '').toString(),
},
});
}
}
@Deprecated('Use [checkHomeserver] instead.')