about summary refs log tree commit diff
path: root/LibMatrix/Services/HomeserverResolverService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'LibMatrix/Services/HomeserverResolverService.cs')
-rw-r--r--LibMatrix/Services/HomeserverResolverService.cs47
1 files changed, 28 insertions, 19 deletions
diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs

index 53cd2dd..ed1d2e3 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs
@@ -9,7 +9,9 @@ using Microsoft.Extensions.Logging.Abstractions; namespace LibMatrix.Services; public class HomeserverResolverService { - private readonly MatrixHttpClient _httpClient = new(); + private readonly MatrixHttpClient _httpClient = new() { + RetryOnNetworkError = false + }; private static readonly SemaphoreCache<WellKnownUris> WellKnownCache = new(); @@ -44,7 +46,17 @@ public class HomeserverResolverService { return res; }); } - + + private async Task<T?> GetFromJsonAsync<T>(string url) { + try { + return await _httpClient.GetFromJsonAsync<T>(url); + } + catch (Exception e) { + _logger.LogWarning(e, "Failed to get JSON from {url}", url); + return default; + } + } + private async Task<string?> _tryResolveClientEndpoint(string homeserver) { ArgumentNullException.ThrowIfNull(homeserver); _logger.LogTrace("Resolving client well-known: {homeserver}", homeserver); @@ -52,14 +64,20 @@ public class HomeserverResolverService { homeserver = homeserver.TrimEnd('/'); // check if homeserver has a client well-known if (homeserver.StartsWith("https://")) { - clientWellKnown = await _httpClient.TryGetFromJsonAsync<ClientWellKnown>($"{homeserver}/.well-known/matrix/client"); + clientWellKnown = await GetFromJsonAsync<ClientWellKnown>($"{homeserver}/.well-known/matrix/client"); + + if (clientWellKnown is null && await MatrixHttpClient.CheckSuccessStatus($"{homeserver}/_matrix/client/versions")) + return homeserver; } else if (homeserver.StartsWith("http://")) { - clientWellKnown = await _httpClient.TryGetFromJsonAsync<ClientWellKnown>($"{homeserver}/.well-known/matrix/client"); + clientWellKnown = await GetFromJsonAsync<ClientWellKnown>($"{homeserver}/.well-known/matrix/client"); + + if (clientWellKnown is null && await MatrixHttpClient.CheckSuccessStatus($"{homeserver}/_matrix/client/versions")) + return homeserver; } else { - clientWellKnown ??= await _httpClient.TryGetFromJsonAsync<ClientWellKnown>($"https://{homeserver}/.well-known/matrix/client"); - clientWellKnown ??= await _httpClient.TryGetFromJsonAsync<ClientWellKnown>($"http://{homeserver}/.well-known/matrix/client"); + clientWellKnown ??= await GetFromJsonAsync<ClientWellKnown>($"https://{homeserver}/.well-known/matrix/client"); + clientWellKnown ??= await GetFromJsonAsync<ClientWellKnown>($"http://{homeserver}/.well-known/matrix/client"); if (clientWellKnown is null) { if (await MatrixHttpClient.CheckSuccessStatus($"https://{homeserver}/_matrix/client/versions")) @@ -84,14 +102,14 @@ public class HomeserverResolverService { homeserver = homeserver.TrimEnd('/'); // check if homeserver has a server well-known if (homeserver.StartsWith("https://")) { - serverWellKnown = await _httpClient.TryGetFromJsonAsync<ServerWellKnown>($"{homeserver}/.well-known/matrix/server"); + serverWellKnown = await GetFromJsonAsync<ServerWellKnown>($"{homeserver}/.well-known/matrix/server"); } else if (homeserver.StartsWith("http://")) { - serverWellKnown = await _httpClient.TryGetFromJsonAsync<ServerWellKnown>($"{homeserver}/.well-known/matrix/server"); + serverWellKnown = await GetFromJsonAsync<ServerWellKnown>($"{homeserver}/.well-known/matrix/server"); } else { - serverWellKnown ??= await _httpClient.TryGetFromJsonAsync<ServerWellKnown>($"https://{homeserver}/.well-known/matrix/server"); - serverWellKnown ??= await _httpClient.TryGetFromJsonAsync<ServerWellKnown>($"http://{homeserver}/.well-known/matrix/server"); + serverWellKnown ??= await GetFromJsonAsync<ServerWellKnown>($"https://{homeserver}/.well-known/matrix/server"); + serverWellKnown ??= await GetFromJsonAsync<ServerWellKnown>($"http://{homeserver}/.well-known/matrix/server"); } _logger.LogInformation("Server well-known for {hs}: {json}", homeserver, serverWellKnown?.ToJson() ?? "null"); @@ -115,15 +133,6 @@ public class HomeserverResolverService { _logger.LogInformation("No server well-known for {server}...", homeserver); return null; } - - [Obsolete("Use authenticated media, available on AuthenticatedHomeserverGeneric", true)] - public async Task<string?> ResolveMediaUri(string homeserver, string mxc) { - if (homeserver is null) throw new ArgumentNullException(nameof(homeserver)); - if (mxc is null) throw new ArgumentNullException(nameof(mxc)); - if (!mxc.StartsWith("mxc://")) throw new InvalidDataException("mxc must start with mxc://"); - homeserver = (await ResolveHomeserverFromWellKnown(homeserver)).Client; - return mxc.Replace("mxc://", $"{homeserver}/_matrix/media/v3/download/"); - } public class WellKnownUris { public string? Client { get; set; }