Fix URI parsing for IPv6 addresses and add tests.

Closes #5057
This commit is contained in:
Jojo-1000
2023-11-10 13:53:10 +01:00
parent ec9d49d684
commit a45ce610df
2 changed files with 51 additions and 8 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ namespace Duplicati.Library.Utility
/// <summary>
/// A very lax version of a URL parser
/// </summary>
private static readonly System.Text.RegularExpressions.Regex URL_PARSER = new System.Text.RegularExpressions.Regex(@"(?<scheme>[^:]+)://(((?<username>[^\:\?/]+)(\:(?<password>[^@\:\?/]*))?\@))?((?<hostname>[^/\?\:]+)(\:(?<port>\d+))?)?((?<path>[^\?]*))?(\?(?<query>.+))?");
private static readonly System.Text.RegularExpressions.Regex URL_PARSER = new System.Text.RegularExpressions.Regex(@"(?<scheme>[^:]+)://(((?<username>[^\:\?/]+)(\:(?<password>[^@\:\?/]*))?\@))?((?<hostname>(?:[^\[/\?\:][^/\?\:]*)|(?:\[[^\]]+\]))(\:(?<port>\d+))?)?((?<path>[^\?]*))?(\?(?<query>.+))?");
/// <summary>
/// The URL scheme, e.g. http
+50 -7
View File
@@ -17,11 +17,11 @@
using NUnit.Framework;
using System.Collections.Specialized;
namespace Duplicati.UnitTest
{
public class UriUtilityTests
{
namespace Duplicati.UnitTest
{
public class UriUtilityTests
{
[Test]
[Category("UriUtility")]
public static void TestBuildUriQuery()
@@ -65,5 +65,48 @@ namespace Duplicati.UnitTest
Assert.AreEqual("/a", Library.Utility.UrlPath.Create(path1).Append(null).ToString());
Assert.AreEqual("/b/", Library.Utility.UrlPath.Create(string.Empty).Append(path2).ToString());
}
}
}
[Test]
[Category("UriUtility")]
public static void TestUriParse(
[Values("[1:2:3::4]", "127.0.0.1", "hostname")] string host,
[Values("", "user@", "user:pw@")] string user,
[Values("", ":80")] string port,
[Values("", "/path")] string path,
[Values("", "?query")] string query)
{
string uriStr = $"http://{user}{host}{port}{path}{query}";
var uri = new Library.Utility.Uri(uriStr);
Assert.AreEqual("http", uri.Scheme);
Assert.AreEqual(host, uri.Host);
if (port.Length != 0)
{
Assert.AreEqual(80, uri.Port);
}
else
{
Assert.AreEqual(-1, uri.Port);
}
Assert.AreEqual(path.TrimStart('/'), uri.Path);
Assert.AreEqual(query.Length == 0 ? null : query.TrimStart('?'), uri.Query);
if (user.Length == 0)
{
Assert.IsNull(uri.Username);
Assert.IsNull(uri.Password);
}
else
{
Assert.AreEqual("user", uri.Username);
if (user.Contains(":"))
{
Assert.AreEqual("pw", uri.Password);
}
else
{
Assert.IsNull(uri.Password);
}
}
}
}
}