Blazor: Decode URL segments in RouteContext (#8759)

* Decode URL segments in RouteContext
This commit is contained in:
Fabien Barbier 2019-06-25 16:27:24 +02:00 committed by Javier Calvarro Nelson
parent c6718d054d
commit bf95c946ec
2 changed files with 24 additions and 2 deletions

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
@ -15,6 +15,11 @@ namespace Microsoft.AspNetCore.Components.Routing
// This is a simplification. We are assuming there are no paths like /a//b/. A proper routing
// implementation would be more sophisticated.
Segments = path.Trim('/').Split(Separator, StringSplitOptions.RemoveEmptyEntries);
// Individual segments are URL-decoded in order to support arbitrary characters, assuming UTF-8 encoding.
for (int i = 0; i < Segments.Length; i++)
{
Segments[i] = Uri.UnescapeDataString(Segments[i]);
}
}
public string[] Segments { get; }
@ -23,4 +28,4 @@ namespace Microsoft.AspNetCore.Components.Routing
public IDictionary<string, object> Parameters { get; set; }
}
}
}

View File

@ -100,6 +100,20 @@ namespace Microsoft.AspNetCore.Components.Test.Routing
Assert.NotNull(context.Handler);
}
[Fact]
public void CanMatchEncodedSegments()
{
// Arrange
var routeTable = new TestRouteTableBuilder().AddRoute("/some/ünicõdē/🛣/").Build();
var context = new RouteContext("/some/%C3%BCnic%C3%B5d%C4%93/%F0%9F%9B%A3");
// Act
routeTable.Route(context);
// Assert
Assert.NotNull(context.Handler);
}
[Fact]
public void DoesNotMatchIfSegmentsDontMatch()
{
@ -155,6 +169,9 @@ namespace Microsoft.AspNetCore.Components.Test.Routing
[Theory]
[InlineData("/value1", "value1")]
[InlineData("/value2/", "value2")]
[InlineData("/d%C3%A9j%C3%A0%20vu", "déjà vu")]
[InlineData("/d%C3%A9j%C3%A0%20vu/", "déjà vu")]
[InlineData("/d%C3%A9j%C3%A0+vu", "déjà+vu")]
public void CanMatchParameterTemplate(string path, string expectedValue)
{
// Arrange