Skip to content

Commit 3dc2be2

Browse files
authored
Static Files cleanup (#10996)
1 parent 1b5db12 commit 3dc2be2

File tree

9 files changed

+93
-10
lines changed

9 files changed

+93
-10
lines changed

src/Middleware/Middleware.sln

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Reques
289289
EndProject
290290
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.RequestThrottling.Tests", "RequestThrottling\test\Microsoft.AspNetCore.RequestThrottling.Tests.csproj", "{353AA2B0-1013-486C-B5BD-9379385CA403}"
291291
EndProject
292+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Metadata", "..\Http\Metadata\src\Microsoft.AspNetCore.Metadata.csproj", "{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}"
293+
EndProject
294+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authorization", "..\Security\Authorization\Core\src\Microsoft.AspNetCore.Authorization.csproj", "{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}"
295+
EndProject
292296
Global
293297
GlobalSection(SolutionConfigurationPlatforms) = preSolution
294298
Debug|Any CPU = Debug|Any CPU
@@ -1571,6 +1575,30 @@ Global
15711575
{353AA2B0-1013-486C-B5BD-9379385CA403}.Release|x64.Build.0 = Release|Any CPU
15721576
{353AA2B0-1013-486C-B5BD-9379385CA403}.Release|x86.ActiveCfg = Release|Any CPU
15731577
{353AA2B0-1013-486C-B5BD-9379385CA403}.Release|x86.Build.0 = Release|Any CPU
1578+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1579+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
1580+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|x64.ActiveCfg = Debug|Any CPU
1581+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|x64.Build.0 = Debug|Any CPU
1582+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|x86.ActiveCfg = Debug|Any CPU
1583+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|x86.Build.0 = Debug|Any CPU
1584+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
1585+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|Any CPU.Build.0 = Release|Any CPU
1586+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|x64.ActiveCfg = Release|Any CPU
1587+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|x64.Build.0 = Release|Any CPU
1588+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|x86.ActiveCfg = Release|Any CPU
1589+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|x86.Build.0 = Release|Any CPU
1590+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1591+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
1592+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|x64.ActiveCfg = Debug|Any CPU
1593+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|x64.Build.0 = Debug|Any CPU
1594+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|x86.ActiveCfg = Debug|Any CPU
1595+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|x86.Build.0 = Debug|Any CPU
1596+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
1597+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|Any CPU.Build.0 = Release|Any CPU
1598+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|x64.ActiveCfg = Release|Any CPU
1599+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|x64.Build.0 = Release|Any CPU
1600+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|x86.ActiveCfg = Release|Any CPU
1601+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|x86.Build.0 = Release|Any CPU
15741602
EndGlobalSection
15751603
GlobalSection(SolutionProperties) = preSolution
15761604
HideSolutionNode = FALSE
@@ -1695,6 +1723,8 @@ Global
16951723
{6720919C-0DEA-49E1-90DC-F1883F7919CD} = {8C9AA8A2-9D1F-4450-9F8D-56BAB6F3D343}
16961724
{4CE2384D-6B88-4824-ADD1-4183D180FEFF} = {8C9AA8A2-9D1F-4450-9F8D-56BAB6F3D343}
16971725
{353AA2B0-1013-486C-B5BD-9379385CA403} = {8C9AA8A2-9D1F-4450-9F8D-56BAB6F3D343}
1726+
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7} = {ACA6DDB9-7592-47CE-A740-D15BF307E9E0}
1727+
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF} = {ACA6DDB9-7592-47CE-A740-D15BF307E9E0}
16981728
EndGlobalSection
16991729
GlobalSection(ExtensibilityGlobals) = postSolution
17001730
SolutionGuid = {83786312-A93B-4BB4-AB06-7C6913A59AFA}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"solution": {
3+
"path": "..\\Middleware.sln",
4+
"projects": [
5+
"..\\Hosting\\Abstractions\\src\\Microsoft.AspNetCore.Hosting.Abstractions.csproj",
6+
"..\\Hosting\\Hosting\\src\\Microsoft.AspNetCore.Hosting.csproj",
7+
"..\\Hosting\\Server.Abstractions\\src\\Microsoft.AspNetCore.Hosting.Server.Abstractions.csproj",
8+
"..\\Hosting\\Server.IntegrationTesting\\src\\Microsoft.AspNetCore.Server.IntegrationTesting.csproj",
9+
"..\\Hosting\\TestHost\\src\\Microsoft.AspNetCore.TestHost.csproj",
10+
"..\\Http\\Authentication.Abstractions\\src\\Microsoft.AspNetCore.Authentication.Abstractions.csproj",
11+
"..\\Http\\Authentication.Core\\src\\Microsoft.AspNetCore.Authentication.Core.csproj",
12+
"..\\Http\\Http.Abstractions\\src\\Microsoft.AspNetCore.Http.Abstractions.csproj",
13+
"..\\Http\\Http.Extensions\\src\\Microsoft.AspNetCore.Http.Extensions.csproj",
14+
"..\\Http\\Http.Features\\src\\Microsoft.AspNetCore.Http.Features.csproj",
15+
"..\\Http\\Metadata\\src\\Microsoft.AspNetCore.Metadata.csproj",
16+
"..\\Http\\Routing.Abstractions\\src\\Microsoft.AspNetCore.Routing.Abstractions.csproj",
17+
"..\\Http\\WebUtilities\\src\\Microsoft.AspNetCore.WebUtilities.csproj",
18+
"..\\Security\\Authorization\\Core\\src\\Microsoft.AspNetCore.Authorization.csproj",
19+
"..\\Servers\\Connections.Abstractions\\src\\Microsoft.AspNetCore.Connections.Abstractions.csproj",
20+
"..\\Servers\\HttpSys\\src\\Microsoft.AspNetCore.Server.HttpSys.csproj",
21+
"..\\Servers\\IIS\\IISIntegration\\src\\Microsoft.AspNetCore.Server.IISIntegration.csproj",
22+
"..\\Servers\\Kestrel\\Core\\src\\Microsoft.AspNetCore.Server.Kestrel.Core.csproj",
23+
"..\\Servers\\Kestrel\\Kestrel\\src\\Microsoft.AspNetCore.Server.Kestrel.csproj",
24+
"..\\Servers\\Kestrel\\Transport.Abstractions\\src\\Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.csproj",
25+
"..\\Servers\\Kestrel\\Transport.Sockets\\src\\Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.csproj",
26+
"..\\http\\Headers\\src\\Microsoft.Net.Http.Headers.csproj",
27+
"..\\http\\Routing\\src\\Microsoft.AspNetCore.Routing.csproj",
28+
"..\\http\\http\\src\\Microsoft.AspNetCore.Http.csproj",
29+
"HttpOverrides\\src\\Microsoft.AspNetCore.HttpOverrides.csproj",
30+
"ResponseCompression\\src\\Microsoft.AspNetCore.ResponseCompression.csproj",
31+
"StaticFiles\\samples\\StaticFileSample\\StaticFileSample.csproj",
32+
"StaticFiles\\src\\Microsoft.AspNetCore.StaticFiles.csproj",
33+
"StaticFiles\\test\\FunctionalTests\\Microsoft.AspNetCore.StaticFiles.FunctionalTests.csproj",
34+
"StaticFiles\\test\\UnitTests\\Microsoft.AspNetCore.StaticFiles.Tests.csproj"
35+
]
36+
}
37+
}

src/Middleware/StaticFiles/samples/StaticFileSample/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public void ConfigureServices(IServiceCollection services)
1717

1818
public void Configure(IApplicationBuilder app, IWebHostEnvironment host)
1919
{
20-
Console.WriteLine("webroot: " + host.WebRootPath);
21-
2220
app.UseResponseCompression();
2321

2422
app.UseFileServer(new FileServerOptions
@@ -37,6 +35,7 @@ public static void Main(string[] args)
3735
})
3836
.UseContentRoot(Directory.GetCurrentDirectory())
3937
.UseKestrel()
38+
// .UseHttpSys()
4039
.UseIISIntegration()
4140
.UseStartup<Startup>()
4241
.Build();

src/Middleware/StaticFiles/samples/StaticFileSample/StaticFileSample.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
2-
32
<PropertyGroup>
43
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
55
</PropertyGroup>
66

77
<ItemGroup>
88
<Reference Include="Microsoft.AspNetCore.ResponseCompression" />
99
<Reference Include="Microsoft.AspNetCore.StaticFiles" />
1010
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
11+
<Reference Include="Microsoft.AspNetCore.Server.HttpSys" />
1112
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
1213
<Reference Include="Microsoft.Extensions.Logging.Console" />
1314
</ItemGroup>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<meta charset="utf-8" />
6+
<title></title>
7+
</head>
8+
<body>
9+
A static HTML file.<br />
10+
</body>
11+
</html>

src/Middleware/StaticFiles/src/DefaultFilesMiddleware.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Builder;
77
using Microsoft.AspNetCore.Hosting;
88
using Microsoft.AspNetCore.Http;
9+
using Microsoft.AspNetCore.Http.Extensions;
910
using Microsoft.Extensions.FileProviders;
1011
using Microsoft.Extensions.Options;
1112
using Microsoft.Net.Http.Headers;
@@ -81,8 +82,10 @@ public Task Invoke(HttpContext context)
8182
// This prevents relative links from breaking.
8283
if (!Helpers.PathEndsInSlash(context.Request.Path))
8384
{
84-
context.Response.StatusCode = 301;
85-
context.Response.Headers[HeaderNames.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
85+
context.Response.StatusCode = StatusCodes.Status301MovedPermanently;
86+
var request = context.Request;
87+
var redirect = UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path + "/", request.QueryString);
88+
context.Response.Headers[HeaderNames.Location] = redirect;
8689
return Task.CompletedTask;
8790
}
8891

src/Middleware/StaticFiles/src/DirectoryBrowserMiddleware.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Builder;
88
using Microsoft.AspNetCore.Hosting;
99
using Microsoft.AspNetCore.Http;
10+
using Microsoft.AspNetCore.Http.Extensions;
1011
using Microsoft.Extensions.FileProviders;
1112
using Microsoft.Extensions.Options;
1213
using Microsoft.Net.Http.Headers;
@@ -88,8 +89,10 @@ public Task Invoke(HttpContext context)
8889
// This prevents relative links from breaking.
8990
if (!Helpers.PathEndsInSlash(context.Request.Path))
9091
{
91-
context.Response.StatusCode = 301;
92-
context.Response.Headers[HeaderNames.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
92+
context.Response.StatusCode = StatusCodes.Status301MovedPermanently;
93+
var request = context.Request;
94+
var redirect = UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path + "/", request.QueryString);
95+
context.Response.Headers[HeaderNames.Location] = redirect;
9396
return Task.CompletedTask;
9497
}
9598

src/Middleware/StaticFiles/test/UnitTests/DefaultFilesMiddlewareTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,8 @@ private async Task NearMatch_RedirectAddSlash(string baseUrl, string baseDir, st
191191

192192
Assert.Equal(HttpStatusCode.Moved, response.StatusCode);
193193
// the url in the header of `Location: /xxx/xxx` should be encoded
194-
var expectedURL = UriHelper.BuildRelative(baseUrl, requestUrl + "/", new QueryString(queryString), new FragmentString());
195194
var actualURL = response.Headers.GetValues("Location").FirstOrDefault();
196-
Assert.Equal(expectedURL, actualURL);
195+
Assert.Equal("http://localhost" + baseUrl + new PathString(requestUrl + "/") + queryString, actualURL);
197196
Assert.Empty((await response.Content.ReadAsByteArrayAsync()));
198197
}
199198
}

src/Middleware/StaticFiles/test/UnitTests/DirectoryBrowserMiddlewareTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private async Task NearMatch_RedirectAddSlash(string baseUrl, string baseDir, st
205205
var response = await server.CreateRequest(requestUrl + queryString).GetAsync();
206206

207207
Assert.Equal(HttpStatusCode.Moved, response.StatusCode);
208-
Assert.Equal(requestUrl + "/" + queryString, response.Headers.GetValues("Location").FirstOrDefault());
208+
Assert.Equal("http://localhost" + requestUrl + "/" + queryString, response.Headers.GetValues("Location").FirstOrDefault());
209209
Assert.Empty((await response.Content.ReadAsByteArrayAsync()));
210210
}
211211
}

0 commit comments

Comments
 (0)