From 5d941506f606d32f7ffff75d6f06c65db75d0634 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Tue, 28 Jan 2025 08:25:26 +0100 Subject: [PATCH] improve `TryGetHandler` to find direct matches before checking via RegEx --- Pilz.Net/Api/ApiServer.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Pilz.Net/Api/ApiServer.cs b/Pilz.Net/Api/ApiServer.cs index b15979b..0807e04 100644 --- a/Pilz.Net/Api/ApiServer.cs +++ b/Pilz.Net/Api/ApiServer.cs @@ -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; }