diff --git a/src/Microsoft.AspNetCore.StaticFiles/DefaultFilesMiddleware.cs b/src/Microsoft.AspNetCore.StaticFiles/DefaultFilesMiddleware.cs index e879bcf..3c3a1c6 100644 --- a/src/Microsoft.AspNetCore.StaticFiles/DefaultFilesMiddleware.cs +++ b/src/Microsoft.AspNetCore.StaticFiles/DefaultFilesMiddleware.cs @@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.StaticFiles for (int matchIndex = 0; matchIndex < _options.DefaultFileNames.Count; matchIndex++) { string defaultFile = _options.DefaultFileNames[matchIndex]; - var file = _fileProvider.GetFileInfo(subpath + defaultFile); + var file = _fileProvider.GetFileInfo(subpath.Value + defaultFile); // TryMatchPath will make sure subpath always ends with a "/" by adding it if needed. if (file.Exists) { diff --git a/test/Microsoft.AspNetCore.StaticFiles.Tests/DefaultFilesMiddlewareTests.cs b/test/Microsoft.AspNetCore.StaticFiles.Tests/DefaultFilesMiddlewareTests.cs index 4601522..7df375d 100644 --- a/test/Microsoft.AspNetCore.StaticFiles.Tests/DefaultFilesMiddlewareTests.cs +++ b/test/Microsoft.AspNetCore.StaticFiles.Tests/DefaultFilesMiddlewareTests.cs @@ -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; @@ -9,6 +9,7 @@ using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.Testing.xunit; using Microsoft.Extensions.FileProviders; @@ -75,6 +76,8 @@ namespace Microsoft.AspNetCore.StaticFiles [InlineData("", @".", "/SubFolder/")] [InlineData("", @"./", "/SubFolder/")] [InlineData("", @"./SubFolder", "/")] + [InlineData("", @"./SubFolder", "/你好/")] + [InlineData("", @"./SubFolder", "/你好/世界/")] public async Task FoundDirectoryWithDefaultFile_PathModified_All(string baseUrl, string baseDir, string requestUrl) { await FoundDirectoryWithDefaultFile_PathModified(baseUrl, baseDir, requestUrl); @@ -85,6 +88,8 @@ namespace Microsoft.AspNetCore.StaticFiles [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData("", @".\", "/SubFolder/")] [InlineData("", @".\subFolder", "/")] + [InlineData("", @".\SubFolder", "/你好/")] + [InlineData("", @".\SubFolder", "/你好/世界/")] public async Task FoundDirectoryWithDefaultFile_PathModified_Windows(string baseUrl, string baseDir, string requestUrl) { await FoundDirectoryWithDefaultFile_PathModified(baseUrl, baseDir, requestUrl); @@ -114,6 +119,8 @@ namespace Microsoft.AspNetCore.StaticFiles [InlineData("", @".", "/SubFolder", "")] [InlineData("", @"./", "/SubFolder", "")] [InlineData("", @"./", "/SubFolder", "?a=b")] + [InlineData("", @"./SubFolder", "/你好", "?a=b")] + [InlineData("", @"./SubFolder", "/你好/世界", "?a=b")] public async Task NearMatch_RedirectAddSlash_All(string baseUrl, string baseDir, string requestUrl, string queryString) { await NearMatch_RedirectAddSlash(baseUrl, baseDir, requestUrl, queryString); @@ -124,6 +131,8 @@ namespace Microsoft.AspNetCore.StaticFiles [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData("", @".\", "/SubFolder", "")] [InlineData("", @".\", "/SubFolder", "?a=b")] + [InlineData("", @".\SubFolder", "/你好", "?a=b")] + [InlineData("", @".\SubFolder", "/你好/世界", "?a=b")] public async Task NearMatch_RedirectAddSlash_Windows(string baseUrl, string baseDir, string requestUrl, string queryString) { await NearMatch_RedirectAddSlash(baseUrl, baseDir, requestUrl, queryString); @@ -141,7 +150,10 @@ namespace Microsoft.AspNetCore.StaticFiles var response = await server.CreateRequest(requestUrl + queryString).GetAsync(); Assert.Equal(HttpStatusCode.Moved, response.StatusCode); - Assert.Equal(requestUrl + "/" + queryString, response.Headers.GetValues("Location").FirstOrDefault()); + // the url in the header of `Location: /xxx/xxx` should be encoded + var expectedURL = UriHelper.BuildRelative(baseUrl, requestUrl + "/", new QueryString(queryString), new FragmentString()); + var actualURL = response.Headers.GetValues("Location").FirstOrDefault(); + Assert.Equal(expectedURL, actualURL); Assert.Empty((await response.Content.ReadAsByteArrayAsync())); } } diff --git a/test/Microsoft.AspNetCore.StaticFiles.Tests/SubFolder/你好/default.html b/test/Microsoft.AspNetCore.StaticFiles.Tests/SubFolder/你好/default.html new file mode 100644 index 0000000..4740d83 --- /dev/null +++ b/test/Microsoft.AspNetCore.StaticFiles.Tests/SubFolder/你好/default.html @@ -0,0 +1,11 @@ + + + + + + + + + Hello World + + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.StaticFiles.Tests/SubFolder/你好/世界/default.html b/test/Microsoft.AspNetCore.StaticFiles.Tests/SubFolder/你好/世界/default.html new file mode 100644 index 0000000..4740d83 --- /dev/null +++ b/test/Microsoft.AspNetCore.StaticFiles.Tests/SubFolder/你好/世界/default.html @@ -0,0 +1,11 @@ + + + + + + + + + Hello World + + \ No newline at end of file