Merge pull request #5058 from Jojo-1000/fix-ipv6-uri
Fix URI parsing for IPv6 addresses
This commit is contained in:
@@ -38,7 +38,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
|
||||
|
||||
@@ -69,5 +69,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user