From 7a14583d6ac7da94f8c442382906678ed55f8429 Mon Sep 17 00:00:00 2001 From: glax Date: Wed, 20 Sep 2023 13:30:52 +0200 Subject: [PATCH] Moved Regex for baseUrl to Globalbase --- Tranga/GlobalBase.cs | 2 ++ Tranga/LibraryConnectors/LibraryConnector.cs | 5 ++--- Tranga/NotificationConnectors/Gotify.cs | 4 +++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Tranga/GlobalBase.cs b/Tranga/GlobalBase.cs index 34b3cd7..89e66f0 100644 --- a/Tranga/GlobalBase.cs +++ b/Tranga/GlobalBase.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text.RegularExpressions; using Logging; using Newtonsoft.Json; using Tranga.LibraryConnectors; @@ -14,6 +15,7 @@ public abstract class GlobalBase protected HashSet libraryConnectors { get; init; } protected List cachedPublications { get; init; } protected static readonly NumberFormatInfo numberFormatDecimalPoint = new (){ NumberDecimalSeparator = "." }; + protected static readonly Regex baseUrlRex = new(@"https?:\/\/[0-9A-z\.]*"); protected GlobalBase(GlobalBase clone) { diff --git a/Tranga/LibraryConnectors/LibraryConnector.cs b/Tranga/LibraryConnectors/LibraryConnector.cs index 5da0131..64ba213 100644 --- a/Tranga/LibraryConnectors/LibraryConnector.cs +++ b/Tranga/LibraryConnectors/LibraryConnector.cs @@ -22,10 +22,9 @@ public abstract class LibraryConnector : GlobalBase protected LibraryConnector(GlobalBase clone, string baseUrl, string auth, LibraryType libraryType) : base(clone) { Log($"Creating libraryConnector {Enum.GetName(libraryType)}"); - Regex urlRex = new(@"https?:\/\/[0-9A-z\.]*"); - if (!urlRex.IsMatch(baseUrl)) + if (!baseUrlRex.IsMatch(baseUrl)) throw new ArgumentException("Base url does not match pattern"); - this.baseUrl = urlRex.Match(baseUrl).Value; + this.baseUrl = baseUrlRex.Match(baseUrl).Value; this.auth = auth; this.libraryType = libraryType; } diff --git a/Tranga/NotificationConnectors/Gotify.cs b/Tranga/NotificationConnectors/Gotify.cs index deb8b57..cbe3d9c 100644 --- a/Tranga/NotificationConnectors/Gotify.cs +++ b/Tranga/NotificationConnectors/Gotify.cs @@ -13,7 +13,9 @@ public class Gotify : NotificationConnector [JsonConstructor] public Gotify(GlobalBase clone, string endpoint, string appToken) : base(clone, NotificationConnectorType.Gotify) { - this.endpoint = endpoint; + if (!baseUrlRex.IsMatch(endpoint)) + throw new ArgumentException("endpoint does not match pattern"); + this.endpoint = baseUrlRex.Match(endpoint).Value;; this.appToken = appToken; }