Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Handle the case where appid can contain some uppercases
Signed-off-by: Nicolas Chaussé <chausse.nicolas@gmail.com>
Signed-off-by: TWEESTY <chausse.nicolas@gmail.com>
Signed-off-by: Nicolas Chaussé <chausse.nicolas@gmail.com>
  • Loading branch information
TWEESTY committed Feb 10, 2024
commit d003688553794b9b7ea034e47ec78fa063d76142
35 changes: 32 additions & 3 deletions src/Dapr.Client/InvocationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,9 @@

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -128,17 +130,44 @@ internal bool TryRewriteUri(Uri? uri, [NotNullWhen(true)] out Uri? rewritten)
return false;
}


var builder = new UriBuilder(uri)
{
Scheme = this.parsedEndpoint.Scheme,
Host = this.parsedEndpoint.Host,
Port = this.parsedEndpoint.Port,
Path = $"/v1.0/invoke/{uri.Host}/method" + uri.AbsolutePath,
Path = $"/v1.0/invoke/{GetOriginalHostFromUri(uri)}/method" + uri.AbsolutePath,
};

rewritten = builder.Uri;
return true;
}

/// <summary>
/// Get the original host (case sensitive) from the URI (thanks to uri.OriginalString)
/// Mandatory to get the original host if the app id has at least one uppercase
/// </summary>
/// <param name="uri">The uri</param>
/// <returns>The original hostname from the uri</returns>
/// <exception cref="ArgumentException">The original string from the uri is invalid</exception>
private string GetOriginalHostFromUri(Uri uri)
{
ArgumentNullException.ThrowIfNull(uri);

// If there is no upper character inside the original string, we can directly return the uri host
if(!uri.OriginalString.Any(char.IsUpper))
{
return uri.Host;
}

Regex regex = new Regex("^.+?://(?<host>[^:/]+)", RegexOptions.Singleline | RegexOptions.Compiled);
Match match = regex.Match(uri.OriginalString);

if (!match.Success || !match.Groups.TryGetValue("host", out Group? host))
{
throw new ArgumentException("The original string for the uri is invalid.", nameof(uri));
}

return host.Value;
}
}
}
7 changes: 6 additions & 1 deletion test/Dapr.Client.Test/InvocationHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -80,11 +80,16 @@ public void TryRewriteUri_FailsForRelativeUris()

[Theory]
[InlineData("http://bank", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData("http://Bank", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData("http://bank:3939", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData("http://Bank:3939", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData("http://app-id.with.dots", "https://some.host:3499/v1.0/invoke/app-id.with.dots/method/")]
[InlineData("http://bank:3939/", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData("http://Bank:3939/", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData("http://bank:3939/some/path", "https://some.host:3499/v1.0/invoke/bank/method/some/path")]
[InlineData("http://Bank:3939/some/path", "https://some.host:3499/v1.0/invoke/Bank/method/some/path")]
[InlineData("http://bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/bank/method/some/path?q=test&p=another#fragment")]
[InlineData("http://Bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/Bank/method/some/path?q=test&p=another#fragment")]
public void TryRewriteUri_RewritesUriToDaprInvoke(string uri, string expected)
{
var handler = new InvocationHandler()
Expand Down