From 885d0d276c738df6a74e02318159bfe8638840ac Mon Sep 17 00:00:00 2001 From: NZSmartie Date: Thu, 7 Sep 2017 22:19:27 +1200 Subject: [PATCH] Include full uri in CORELink format when Authoraty does not match. --- CoAPNet/CoreLinkFormat.cs | 24 ++++++++++++++++-------- CoAPNet/Utils/UriExtensions.cs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 CoAPNet/Utils/UriExtensions.cs diff --git a/CoAPNet/CoreLinkFormat.cs b/CoAPNet/CoreLinkFormat.cs index 8308d98..1120f1c 100644 --- a/CoAPNet/CoreLinkFormat.cs +++ b/CoAPNet/CoreLinkFormat.cs @@ -18,6 +18,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using CoAPNet.Utils; namespace CoAPNet { @@ -217,14 +218,21 @@ public static List Parse(string message) private static readonly Uri _throwAwayUri = new Uri("coap://localhost/"); - public static string ToCoreLinkFormat(CoapResourceMetadata resource) + public static string ToCoreLinkFormat(CoapResourceMetadata resource, Uri baseUri = null) { var message = new StringBuilder(); try { - message.Append(resource.UriReference.IsAbsoluteUri - ? $"<{resource.UriReference.AbsolutePath}>" - : $"<{new Uri(_throwAwayUri, resource.UriReference).AbsolutePath}>"); + if (baseUri == null) + baseUri = _throwAwayUri; + + var uri = resource.UriReference.IsAbsoluteUri + ? resource.UriReference + : new Uri(baseUri, resource.UriReference); + + message.Append(CoapUri.Compare(uri, baseUri, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase) == 0 + ? $"<{uri.AbsolutePath}>" + : $"<{uri}>"); if(resource.InterfaceDescription.Count > 0) message.AppendFormat(";if=\"{0}\"", string.Join(" ", resource.InterfaceDescription)); @@ -248,8 +256,8 @@ public static string ToCoreLinkFormat(CoapResourceMetadata resource) if (resource.Rel.Count > 1) message.AppendFormat(";rel=\"{0}\"", string.Join(" ", resource.Rel)); - if ((resource.Anchor ?? string.Empty) != string.Empty) - message.Append($";anchor={resource.Anchor}"); + if (!string.IsNullOrEmpty(resource.Anchor)) + message.Append($";anchor=\"{resource.Anchor}\""); if ((resource.HrefLang ?? string.Empty) != string.Empty) message.Append($";hreflang={resource.HrefLang?.ToLower()}"); @@ -317,9 +325,9 @@ public static string ToCoreLinkFormat(CoapResourceMetadata resource) return message.ToString(); } - public static string ToCoreLinkFormat(IEnumerable resources) + public static string ToCoreLinkFormat(IEnumerable resources, Uri baseUri = null) { - return string.Join(",", resources.Select(ToCoreLinkFormat)); + return string.Join(",", resources.Select(r => ToCoreLinkFormat(r, baseUri))); } } } diff --git a/CoAPNet/Utils/UriExtensions.cs b/CoAPNet/Utils/UriExtensions.cs new file mode 100644 index 0000000..c7eda67 --- /dev/null +++ b/CoAPNet/Utils/UriExtensions.cs @@ -0,0 +1,33 @@ +using System; +using System.Linq; + +namespace CoAPNet.Utils +{ + // TODO: This helper class will be redundant in .Net Core 2.0 through sub-classing HttpStyleUriParser and adding CoAP defaults. + public static class CoapUri + { + private static readonly string[] _schemes = {"coap", "coaps"}; + + public static int Compare(Uri uri1, Uri uri2, UriComponents partsToCompare, UriFormat compareFormat, StringComparison comparisonType) + { + // Setup Default ports before performing comparasons. + if (_schemes.Contains(uri1.Scheme.ToLower()) && uri1.Port == -1) + uri1 = new UriBuilder(uri1) + { + Port = uri1.Scheme == "coap" + ? Coap.Port + : Coap.PortDTLS + }.Uri; + + if (_schemes.Contains(uri2.Scheme.ToLower()) && uri2.Port == -1) + uri2 = new UriBuilder(uri2) + { + Port = uri2.Scheme == "coap" + ? Coap.Port + : Coap.PortDTLS + }.Uri; + + return Uri.Compare(uri1, uri2, partsToCompare, compareFormat, comparisonType); + } + } +} \ No newline at end of file