Skip to content

Commit

Permalink
Include full uri in CORELink format when Authoraty does not match.
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Sep 7, 2017
1 parent b58234a commit 885d0d2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
24 changes: 16 additions & 8 deletions CoAPNet/CoreLinkFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoAPNet.Utils;

namespace CoAPNet
{
Expand Down Expand Up @@ -217,14 +218,21 @@ public static List<CoapResourceMetadata> 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));
Expand All @@ -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()}");
Expand Down Expand Up @@ -317,9 +325,9 @@ public static string ToCoreLinkFormat(CoapResourceMetadata resource)
return message.ToString();
}

public static string ToCoreLinkFormat(IEnumerable<CoapResourceMetadata> resources)
public static string ToCoreLinkFormat(IEnumerable<CoapResourceMetadata> resources, Uri baseUri = null)
{
return string.Join(",", resources.Select(ToCoreLinkFormat));
return string.Join(",", resources.Select(r => ToCoreLinkFormat(r, baseUri)));
}
}
}
33 changes: 33 additions & 0 deletions CoAPNet/Utils/UriExtensions.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 885d0d2

Please sign in to comment.