Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit a435872

Browse files
itminusTratcher
authored andcommitted
fix : DefaultFilesMiddleware serve with unicode path (#257)
1 parent 7a3a936 commit a435872

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

src/Microsoft.AspNetCore.StaticFiles/DefaultFilesMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public Task Invoke(HttpContext context)
7272
for (int matchIndex = 0; matchIndex < _options.DefaultFileNames.Count; matchIndex++)
7373
{
7474
string defaultFile = _options.DefaultFileNames[matchIndex];
75-
var file = _fileProvider.GetFileInfo(subpath + defaultFile);
75+
var file = _fileProvider.GetFileInfo(subpath.Value + defaultFile);
7676
// TryMatchPath will make sure subpath always ends with a "/" by adding it if needed.
7777
if (file.Exists)
7878
{

test/Microsoft.AspNetCore.StaticFiles.Tests/DefaultFilesMiddlewareTests.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using Microsoft.AspNetCore.Builder;
1111
using Microsoft.AspNetCore.Http;
12+
using Microsoft.AspNetCore.Http.Extensions;
1213
using Microsoft.AspNetCore.TestHost;
1314
using Microsoft.AspNetCore.Testing.xunit;
1415
using Microsoft.Extensions.FileProviders;
@@ -75,6 +76,8 @@ private async Task NoMatch_PassesThrough(string baseUrl, string baseDir, string
7576
[InlineData("", @".", "/SubFolder/")]
7677
[InlineData("", @"./", "/SubFolder/")]
7778
[InlineData("", @"./SubFolder", "/")]
79+
[InlineData("", @"./SubFolder", "/你好/")]
80+
[InlineData("", @"./SubFolder", "/你好/世界/")]
7881
public async Task FoundDirectoryWithDefaultFile_PathModified_All(string baseUrl, string baseDir, string requestUrl)
7982
{
8083
await FoundDirectoryWithDefaultFile_PathModified(baseUrl, baseDir, requestUrl);
@@ -85,6 +88,8 @@ public async Task FoundDirectoryWithDefaultFile_PathModified_All(string baseUrl,
8588
[OSSkipCondition(OperatingSystems.MacOSX)]
8689
[InlineData("", @".\", "/SubFolder/")]
8790
[InlineData("", @".\subFolder", "/")]
91+
[InlineData("", @".\SubFolder", "/你好/")]
92+
[InlineData("", @".\SubFolder", "/你好/世界/")]
8893
public async Task FoundDirectoryWithDefaultFile_PathModified_Windows(string baseUrl, string baseDir, string requestUrl)
8994
{
9095
await FoundDirectoryWithDefaultFile_PathModified(baseUrl, baseDir, requestUrl);
@@ -114,6 +119,8 @@ private async Task FoundDirectoryWithDefaultFile_PathModified(string baseUrl, st
114119
[InlineData("", @".", "/SubFolder", "")]
115120
[InlineData("", @"./", "/SubFolder", "")]
116121
[InlineData("", @"./", "/SubFolder", "?a=b")]
122+
[InlineData("", @"./SubFolder", "/你好", "?a=b")]
123+
[InlineData("", @"./SubFolder", "/你好/世界", "?a=b")]
117124
public async Task NearMatch_RedirectAddSlash_All(string baseUrl, string baseDir, string requestUrl, string queryString)
118125
{
119126
await NearMatch_RedirectAddSlash(baseUrl, baseDir, requestUrl, queryString);
@@ -124,6 +131,8 @@ public async Task NearMatch_RedirectAddSlash_All(string baseUrl, string baseDir,
124131
[OSSkipCondition(OperatingSystems.MacOSX)]
125132
[InlineData("", @".\", "/SubFolder", "")]
126133
[InlineData("", @".\", "/SubFolder", "?a=b")]
134+
[InlineData("", @".\SubFolder", "/你好", "?a=b")]
135+
[InlineData("", @".\SubFolder", "/你好/世界", "?a=b")]
127136
public async Task NearMatch_RedirectAddSlash_Windows(string baseUrl, string baseDir, string requestUrl, string queryString)
128137
{
129138
await NearMatch_RedirectAddSlash(baseUrl, baseDir, requestUrl, queryString);
@@ -141,7 +150,10 @@ private async Task NearMatch_RedirectAddSlash(string baseUrl, string baseDir, st
141150
var response = await server.CreateRequest(requestUrl + queryString).GetAsync();
142151

143152
Assert.Equal(HttpStatusCode.Moved, response.StatusCode);
144-
Assert.Equal(requestUrl + "/" + queryString, response.Headers.GetValues("Location").FirstOrDefault());
153+
// the url in the header of `Location: /xxx/xxx` should be encoded
154+
var expectedURL = UriHelper.BuildRelative(baseUrl, requestUrl + "/", new QueryString(queryString), new FragmentString());
155+
var actualURL = response.Headers.GetValues("Location").FirstOrDefault();
156+
Assert.Equal(expectedURL, actualURL);
145157
Assert.Empty((await response.Content.ReadAsByteArrayAsync()));
146158
}
147159
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
3+
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
4+
<head>
5+
<meta charset="utf-8" />
6+
<title></title>
7+
</head>
8+
<body>
9+
Hello World
10+
</body>
11+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
3+
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
4+
<head>
5+
<meta charset="utf-8" />
6+
<title></title>
7+
</head>
8+
<body>
9+
Hello World
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)