From e86353a412b465fa5af9ca803260ea3c4cca64d0 Mon Sep 17 00:00:00 2001 From: Sorunome Date: Sun, 11 Jul 2021 12:26:48 +0200 Subject: [PATCH] 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. --- lib/src/client.dart | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 445b74b6..577b67ec 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -296,16 +296,26 @@ class Client extends MatrixApi { Future getWellKnownInformationsByUserId( String MatrixIdOrDomain, ) async { - final response = await http - .get(Uri.https(MatrixIdOrDomain.domain, '/.well-known/matrix/client')); - var respBody = response.body; try { - respBody = utf8.decode(response.bodyBytes); + final response = await http.get( + Uri.https(MatrixIdOrDomain.domain, '/.well-known/matrix/client')); + var respBody = response.body; + try { + respBody = utf8.decode(response.bodyBytes); + } catch (_) { + // No-OP + } + final rawJson = json.decode(respBody); + return WellKnownInformation.fromJson(rawJson); } catch (_) { - // No-OP + // we got an error processing or fetching the well-known information, let's + // provide a reasonable fallback. + return WellKnownInformation.fromJson({ + 'm.homeserver': { + 'base_url': Uri.https(MatrixIdOrDomain.domain, '').toString(), + }, + }); } - final rawJson = json.decode(respBody); - return WellKnownInformation.fromJson(rawJson); } @Deprecated('Use [checkHomeserver] instead.')