improve TryGetHandler to find direct matches before checking via RegEx

This commit is contained in:
Pilzinsel64
2025-01-28 08:25:26 +01:00
parent 1b1f92dea5
commit 5d941506f6

View File

@@ -408,16 +408,14 @@ public class ApiServer(string apiUrl) : IApiServer
protected virtual bool TryGetHandler(string url, string? query, string method, [NotNullWhen(true)] out PrivateMessageHandler? handler)
{
handler = handlers.FirstOrDefault(handler =>
{
if (!handler.Attribute.Methods.Contains(method))
return false;
// Filter by method
var filtered = handlers.Where(handler => handler.Attribute.Methods.Contains(method));
if (handler.UseRegEx)
return Regex.IsMatch(url, handler.Url, RegexOptions.IgnoreCase);
// Check if equals via string comparation (ignore-case)
handler = filtered.FirstOrDefault(handler => handler.Url.Equals(url, StringComparison.InvariantCultureIgnoreCase));
return handler.Url.Equals(url, StringComparison.InvariantCultureIgnoreCase);
});
// Check if equals via RegEx
handler ??= filtered.Where(n => n.UseRegEx).FirstOrDefault(handler => handler.Url.Equals(url, StringComparison.InvariantCultureIgnoreCase));
return handler != null;
}