diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs
index dc8e047..53cd2dd 100644
--- a/LibMatrix/Services/HomeserverResolverService.cs
+++ b/LibMatrix/Services/HomeserverResolverService.cs
@@ -1,8 +1,4 @@
-using System.Collections.Concurrent;
using System.Diagnostics;
-using System.Net.Http.Json;
-using System.Text.Json;
-using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using ArcaneLibs.Collections;
using ArcaneLibs.Extensions;
@@ -13,9 +9,7 @@ using Microsoft.Extensions.Logging.Abstractions;
namespace LibMatrix.Services;
public class HomeserverResolverService {
- private readonly MatrixHttpClient _httpClient = new() {
- // Timeout = TimeSpan.FromSeconds(60) // TODO: Re-implement this
- };
+ private readonly MatrixHttpClient _httpClient = new();
private static readonly SemaphoreCache<WellKnownUris> WellKnownCache = new();
@@ -26,53 +20,36 @@ public class HomeserverResolverService {
if (logger is NullLogger<HomeserverResolverService>) {
var stackFrame = new StackTrace(true).GetFrame(1);
Console.WriteLine(
- $"WARN | Null logger provided to HomeserverResolverService!\n{stackFrame.GetMethod().DeclaringType} at {stackFrame.GetFileName()}:{stackFrame.GetFileLineNumber()}");
+ $"WARN | Null logger provided to HomeserverResolverService!\n{stackFrame?.GetMethod()?.DeclaringType?.ToString() ?? "null"} at {stackFrame?.GetFileName() ?? "null"}:{stackFrame?.GetFileLineNumber().ToString() ?? "null"}");
}
}
- // private static SemaphoreSlim _wellKnownSemaphore = new(1, 1);
-
public async Task<WellKnownUris> ResolveHomeserverFromWellKnown(string homeserver, bool enableClient = true, bool enableServer = true) {
ArgumentNullException.ThrowIfNull(homeserver);
return await WellKnownCache.GetOrAdd(homeserver, async () => {
- // await _wellKnownSemaphore.WaitAsync();
_logger.LogTrace($"Resolving homeserver well-knowns: {homeserver}");
var client = enableClient ? _tryResolveClientEndpoint(homeserver) : null;
var server = enableServer ? _tryResolveServerEndpoint(homeserver) : null;
var res = new WellKnownUris();
- // try {
if (client != null)
- res.Client = await client ?? throw new Exception($"Could not resolve client URL for {homeserver}.");
- // }
- // catch (Exception e) {
- // _logger.LogError(e, "Error resolving client well-known for {hs}", homeserver);
- // }
+ res.Client = (await client)?.TrimEnd('/') ?? throw new Exception($"Could not resolve client URL for {homeserver}.");
- // try {
if (server != null)
- res.Server = await server ?? throw new Exception($"Could not resolve server URL for {homeserver}.");
- // }
- // catch (Exception e) {
- // _logger.LogError(e, "Error resolving server well-known for {hs}", homeserver);
- // }
+ res.Server = (await server)?.TrimEnd('/') ?? throw new Exception($"Could not resolve server URL for {homeserver}.");
_logger.LogInformation("Resolved well-knowns for {hs}: {json}", homeserver, res.ToJson(indent: false));
- // _wellKnownSemaphore.Release();
return res;
});
}
- // private async Task<WellKnownUris> InternalResolveHomeserverFromWellKnown(string homeserver) {
-
- // }
-
private async Task<string?> _tryResolveClientEndpoint(string homeserver) {
ArgumentNullException.ThrowIfNull(homeserver);
_logger.LogTrace("Resolving client well-known: {homeserver}", homeserver);
ClientWellKnown? clientWellKnown = null;
+ 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");
@@ -104,6 +81,7 @@ public class HomeserverResolverService {
ArgumentNullException.ThrowIfNull(homeserver);
_logger.LogTrace($"Resolving server well-known: {homeserver}");
ServerWellKnown? serverWellKnown = null;
+ 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");
@@ -119,7 +97,7 @@ public class HomeserverResolverService {
_logger.LogInformation("Server well-known for {hs}: {json}", homeserver, serverWellKnown?.ToJson() ?? "null");
if (!string.IsNullOrWhiteSpace(serverWellKnown?.Homeserver)) {
- var resolved = serverWellKnown.Homeserver;
+ var resolved = serverWellKnown.Homeserver.TrimEnd('/');
if (resolved.StartsWith("https://") || resolved.StartsWith("http://"))
return resolved;
if (await MatrixHttpClient.CheckSuccessStatus($"https://{resolved}/_matrix/federation/v1/version"))
@@ -130,14 +108,15 @@ public class HomeserverResolverService {
}
// fallback: most servers host C2S and S2S on the same domain
- var clientUrl = await _tryResolveClientEndpoint(homeserver);
+ var clientUrl = (await _tryResolveClientEndpoint(homeserver)).TrimEnd('/');
if (clientUrl is not null && await MatrixHttpClient.CheckSuccessStatus($"{clientUrl}/_matrix/federation/v1/version"))
return clientUrl;
_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));
|