Skip to content

Commit

Permalink
Using Uri.TryCreate causes regression with namespace uri that use mix…
Browse files Browse the repository at this point in the history
…ed lower/uppercase letters in the <hostname> of the Uri.(#2837)

Uri.TryCreate lower cases the hostnames. Switch always back to the legacy implementation which maintains the casing.
  • Loading branch information
KircMax authored Nov 7, 2024
1 parent ff3674f commit 56dd06d
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions Stack/Opc.Ua.Core/Types/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Security.Cryptography.X509Certificates;
using System.Reflection;
using System.Runtime.Serialization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Net;
using System.Collections.ObjectModel;
using Opc.Ua.Security.Certificates;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Opc.Ua.Security.Certificates;

namespace Opc.Ua
{
Expand Down Expand Up @@ -1267,37 +1267,30 @@ public static string EscapeUri(string uri)
{
if (!string.IsNullOrWhiteSpace(uri))
{
// back compat: for not well formed Uri, fall back to legacy formatting behavior - see #2793
if (!Uri.IsWellFormedUriString(uri, UriKind.Absolute) ||
!Uri.TryCreate(uri.Replace(";", "%3b"), UriKind.Absolute, out Uri validUri))
// always use back compat: for not well formed Uri, fall back to legacy formatting behavior - see #2793, #2826
// problem with Uri.TryCreate(uri.Replace(";", "%3b"), UriKind.Absolute, out Uri validUri);
// -> uppercase letters will later be lowercase (and therefore the uri will later be non-matching)
var buffer = new StringBuilder();
foreach (char ch in uri)
{
var buffer = new StringBuilder();
foreach (char ch in uri)
switch (ch)
{
switch (ch)
case ';':
case '%':
{
case ';':
case '%':
{
buffer.AppendFormat(CultureInfo.InvariantCulture, "%{0:X2}", Convert.ToInt16(ch));
break;
}
buffer.AppendFormat(CultureInfo.InvariantCulture, "%{0:X2}", Convert.ToInt16(ch));
break;
}

default:
{
buffer.Append(ch);
break;
}
default:
{
buffer.Append(ch);
break;
}
}
return buffer.ToString();
}
else
{
return validUri.AbsoluteUri;
}
return buffer.ToString();
}

return String.Empty;
}

Expand Down Expand Up @@ -2604,7 +2597,7 @@ public static void UpdateExtension<T>(ref XmlElementCollection extensions, XmlQu
extensions.Add(document.DocumentElement);
}
}
#endregion
#endregion

#region Reflection Helper Functions
/// <summary>
Expand Down

0 comments on commit 56dd06d

Please sign in to comment.