[backend/webfinger] Catch errors in GetWebFingerTemplateFromHostMetaAsync

This commit is contained in:
Laura Hausmann 2024-02-07 23:11:27 +01:00
parent 6ed944156f
commit b5bfc9a080
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -72,21 +72,27 @@ public class WebFingerService(HttpClient client, HttpRequestService httpRqSvc) {
}
private async Task<string?> GetWebFingerTemplateFromHostMetaAsync(string hostMetaUrl) {
using var res = await client.SendAsync(httpRqSvc.Get(hostMetaUrl, ["application/xrd+xml"]), HttpCompletionOption.ResponseHeadersRead);
using var stream = await res.Content.ReadAsStreamAsync();
try {
using var res = await client.SendAsync(httpRqSvc.Get(hostMetaUrl, ["application/xrd+xml"]),
HttpCompletionOption.ResponseHeadersRead);
using var stream = await res.Content.ReadAsStreamAsync();
var xml = new XmlDocument();
xml.Load(stream);
var xml = new XmlDocument();
xml.Load(stream);
var section = xml["XRD"]?.GetElementsByTagName("Link");
if (section == null) return null;
var section = xml["XRD"]?.GetElementsByTagName("Link");
if (section == null) return null;
//TODO: implement https://stackoverflow.com/a/37322614/18402176 instead
//TODO: implement https://stackoverflow.com/a/37322614/18402176 instead
for (var i = 0; i < section.Count; i++)
if (section[i]?.Attributes?["rel"]?.InnerText == "lrdd")
return section[i]?.Attributes?["template"]?.InnerText;
for (var i = 0; i < section.Count; i++)
if (section[i]?.Attributes?["rel"]?.InnerText == "lrdd")
return section[i]?.Attributes?["template"]?.InnerText;
return null;
return null;
}
catch {
return null;
}
}
}