From e622363947a940e8be05dcd4a5ee1f2eb9a5930e Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Thu, 17 Sep 2020 16:18:22 +0300 Subject: [PATCH 1/4] Add test data --- src/UriTemplateTests/UriTemplateTableTests.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/UriTemplateTests/UriTemplateTableTests.cs b/src/UriTemplateTests/UriTemplateTableTests.cs index e661205..f36a2a3 100644 --- a/src/UriTemplateTests/UriTemplateTableTests.cs +++ b/src/UriTemplateTests/UriTemplateTableTests.cs @@ -21,7 +21,9 @@ public class UriTemplateTableTests InlineData("/baz/kit", "kit"), InlineData("/baz/fod", "baz"), InlineData("/baz/fod/blob", "blob"), - InlineData("/glah/flid/blob", "goo")] + InlineData("/glah/flid/blob", "goo"), + InlineData("/settings/{id}", "set"), + InlineData("/organization/{id}/settings/iteminsights", "org")] public void FindPathTemplates(string url, string key) { var table = new UriTemplateTable(); // Shorter paths and literal path segments should be added to the table first. @@ -31,6 +33,8 @@ public void FindPathTemplates(string url, string key) table.Add("baz", new UriTemplate("/baz/{bar}")); table.Add("blob", new UriTemplate("/baz/{bar}/blob")); table.Add("goo", new UriTemplate("/{goo}/{bar}/blob")); + table.Add("set", new UriTemplate("/settings/{id}")); + table.Add("org", new UriTemplate("/organization/{id}/settings/iteminsights")); var result = table.Match(new Uri(url, UriKind.RelativeOrAbsolute)); From 81dafecc2d35cec6f3d616a6c749e23205b907fa Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Tue, 22 Sep 2020 16:06:49 +0300 Subject: [PATCH 2/4] Add negative lookbehind to ensure strict regex match --- src/UriTemplates/UriTemplate.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/UriTemplates/UriTemplate.cs b/src/UriTemplates/UriTemplate.cs index 70ffc79..e1f99f6 100644 --- a/src/UriTemplates/UriTemplate.cs +++ b/src/UriTemplates/UriTemplate.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -409,7 +409,7 @@ public static string CreateMatchingRegex(string uriTemplate) }); - return regex +"$"; + return "(? Date: Tue, 22 Sep 2020 16:16:15 +0300 Subject: [PATCH 3/4] Remove unused usings and whitespace format --- src/UriTemplates/UriTemplate.cs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/UriTemplates/UriTemplate.cs b/src/UriTemplates/UriTemplate.cs index e1f99f6..85623f1 100644 --- a/src/UriTemplates/UriTemplate.cs +++ b/src/UriTemplates/UriTemplate.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Text.RegularExpressions; namespace Tavis.UriTemplates @@ -36,7 +32,7 @@ public class UriTemplate private readonly string _template; private readonly Dictionary _Parameters; private enum States { CopyingLiterals, ParsingExpression } - + private readonly bool _resolvePartially; @@ -191,8 +187,8 @@ private void ProcessExpression(StringBuilder currentExpression, Result result) varSpec = new VarSpec(op); if (success || !isFirst || _resolvePartially) varSpec.First = false; if (!success && _resolvePartially) {result.Append(",") ; } - break; - + break; + default: if (IsVarNameChar(currentChar)) @@ -406,7 +402,7 @@ public static string CreateMatchingRegex(string uriTemplate) default: return GetExpression(paramNames); } - + }); return "(? paramNames, string prefix) sb.Append("(?:"); sb.Append(paramname); sb.Append("="); - + sb.Append("(?<"); sb.Append(paramname); sb.Append(">"); @@ -519,8 +515,8 @@ private static string GetExpression(List paramNames, string prefix = nul return sb.ToString(); } - + } - + } From e06adc94e49c52b844cdc42a206eed0d4bfe08dd Mon Sep 17 00:00:00 2001 From: Irvine Sunday Date: Mon, 5 Oct 2020 14:46:23 +0300 Subject: [PATCH 4/4] Match relative and absolute paths with regex negative lookbehind --- src/UriTemplateTests/UriTemplateTableTests.cs | 3 ++- src/UriTemplates/UriTemplate.cs | 2 +- src/UriTemplates/UriTemplateTable.cs | 17 ++++++++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/UriTemplateTests/UriTemplateTableTests.cs b/src/UriTemplateTests/UriTemplateTableTests.cs index f36a2a3..652ed09 100644 --- a/src/UriTemplateTests/UriTemplateTableTests.cs +++ b/src/UriTemplateTests/UriTemplateTableTests.cs @@ -18,7 +18,7 @@ public class UriTemplateTableTests [Theory, InlineData("/","root"), InlineData("/baz/fod/burg",""), - InlineData("/baz/kit", "kit"), + InlineData("http://www.example.com/baz/kit", "kit"), InlineData("/baz/fod", "baz"), InlineData("/baz/fod/blob", "blob"), InlineData("/glah/flid/blob", "goo"), @@ -36,6 +36,7 @@ public void FindPathTemplates(string url, string key) table.Add("set", new UriTemplate("/settings/{id}")); table.Add("org", new UriTemplate("/organization/{id}/settings/iteminsights")); + var uri = new Uri(url, UriKind.RelativeOrAbsolute); var result = table.Match(new Uri(url, UriKind.RelativeOrAbsolute)); diff --git a/src/UriTemplates/UriTemplate.cs b/src/UriTemplates/UriTemplate.cs index 85623f1..142b302 100644 --- a/src/UriTemplates/UriTemplate.cs +++ b/src/UriTemplates/UriTemplate.cs @@ -405,7 +405,7 @@ public static string CreateMatchingRegex(string uriTemplate) }); - return "(? _Templates = new Dictionary(); - + public void Add(string key, UriTemplate template) { _Templates.Add(key,template); @@ -16,9 +16,20 @@ public void Add(string key, UriTemplate template) public TemplateMatch Match(Uri url) { - foreach (var template in _Templates ) + if (url == null) + { + throw new ArgumentNullException(nameof(url), "Value cannot be null."); + } + + Uri absolutePath = url; + if (url.IsAbsoluteUri) + { + absolutePath = new Uri(url.AbsolutePath, UriKind.Relative); + } + + foreach (var template in _Templates) { - var parameters = template.Value.GetParameters(url); + var parameters = template.Value.GetParameters(absolutePath); if (parameters != null) { return new TemplateMatch() { Key = template.Key, Parameters = parameters, Template = template.Value };