Skip to content

Static Files cleanup #10996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions src/Middleware/Middleware.sln
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Reques
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.RequestThrottling.Tests", "RequestThrottling\test\Microsoft.AspNetCore.RequestThrottling.Tests.csproj", "{353AA2B0-1013-486C-B5BD-9379385CA403}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Metadata", "..\Http\Metadata\src\Microsoft.AspNetCore.Metadata.csproj", "{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authorization", "..\Security\Authorization\Core\src\Microsoft.AspNetCore.Authorization.csproj", "{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1571,6 +1575,30 @@ Global
{353AA2B0-1013-486C-B5BD-9379385CA403}.Release|x64.Build.0 = Release|Any CPU
{353AA2B0-1013-486C-B5BD-9379385CA403}.Release|x86.ActiveCfg = Release|Any CPU
{353AA2B0-1013-486C-B5BD-9379385CA403}.Release|x86.Build.0 = Release|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|x64.ActiveCfg = Debug|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|x64.Build.0 = Debug|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|x86.ActiveCfg = Debug|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Debug|x86.Build.0 = Debug|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|Any CPU.Build.0 = Release|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|x64.ActiveCfg = Release|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|x64.Build.0 = Release|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|x86.ActiveCfg = Release|Any CPU
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}.Release|x86.Build.0 = Release|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|x64.ActiveCfg = Debug|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|x64.Build.0 = Debug|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|x86.ActiveCfg = Debug|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Debug|x86.Build.0 = Debug|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|Any CPU.Build.0 = Release|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|x64.ActiveCfg = Release|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|x64.Build.0 = Release|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|x86.ActiveCfg = Release|Any CPU
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1695,6 +1723,8 @@ Global
{6720919C-0DEA-49E1-90DC-F1883F7919CD} = {8C9AA8A2-9D1F-4450-9F8D-56BAB6F3D343}
{4CE2384D-6B88-4824-ADD1-4183D180FEFF} = {8C9AA8A2-9D1F-4450-9F8D-56BAB6F3D343}
{353AA2B0-1013-486C-B5BD-9379385CA403} = {8C9AA8A2-9D1F-4450-9F8D-56BAB6F3D343}
{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7} = {ACA6DDB9-7592-47CE-A740-D15BF307E9E0}
{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF} = {ACA6DDB9-7592-47CE-A740-D15BF307E9E0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {83786312-A93B-4BB4-AB06-7C6913A59AFA}
Expand Down
37 changes: 37 additions & 0 deletions src/Middleware/StaticFiles/StaticFiles.slnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"solution": {
"path": "..\\Middleware.sln",
"projects": [
"..\\Hosting\\Abstractions\\src\\Microsoft.AspNetCore.Hosting.Abstractions.csproj",
"..\\Hosting\\Hosting\\src\\Microsoft.AspNetCore.Hosting.csproj",
"..\\Hosting\\Server.Abstractions\\src\\Microsoft.AspNetCore.Hosting.Server.Abstractions.csproj",
"..\\Hosting\\Server.IntegrationTesting\\src\\Microsoft.AspNetCore.Server.IntegrationTesting.csproj",
"..\\Hosting\\TestHost\\src\\Microsoft.AspNetCore.TestHost.csproj",
"..\\Http\\Authentication.Abstractions\\src\\Microsoft.AspNetCore.Authentication.Abstractions.csproj",
"..\\Http\\Authentication.Core\\src\\Microsoft.AspNetCore.Authentication.Core.csproj",
"..\\Http\\Http.Abstractions\\src\\Microsoft.AspNetCore.Http.Abstractions.csproj",
"..\\Http\\Http.Extensions\\src\\Microsoft.AspNetCore.Http.Extensions.csproj",
"..\\Http\\Http.Features\\src\\Microsoft.AspNetCore.Http.Features.csproj",
"..\\Http\\Metadata\\src\\Microsoft.AspNetCore.Metadata.csproj",
"..\\Http\\Routing.Abstractions\\src\\Microsoft.AspNetCore.Routing.Abstractions.csproj",
"..\\Http\\WebUtilities\\src\\Microsoft.AspNetCore.WebUtilities.csproj",
"..\\Security\\Authorization\\Core\\src\\Microsoft.AspNetCore.Authorization.csproj",
"..\\Servers\\Connections.Abstractions\\src\\Microsoft.AspNetCore.Connections.Abstractions.csproj",
"..\\Servers\\HttpSys\\src\\Microsoft.AspNetCore.Server.HttpSys.csproj",
"..\\Servers\\IIS\\IISIntegration\\src\\Microsoft.AspNetCore.Server.IISIntegration.csproj",
"..\\Servers\\Kestrel\\Core\\src\\Microsoft.AspNetCore.Server.Kestrel.Core.csproj",
"..\\Servers\\Kestrel\\Kestrel\\src\\Microsoft.AspNetCore.Server.Kestrel.csproj",
"..\\Servers\\Kestrel\\Transport.Abstractions\\src\\Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.csproj",
"..\\Servers\\Kestrel\\Transport.Sockets\\src\\Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.csproj",
"..\\http\\Headers\\src\\Microsoft.Net.Http.Headers.csproj",
"..\\http\\Routing\\src\\Microsoft.AspNetCore.Routing.csproj",
"..\\http\\http\\src\\Microsoft.AspNetCore.Http.csproj",
"HttpOverrides\\src\\Microsoft.AspNetCore.HttpOverrides.csproj",
"ResponseCompression\\src\\Microsoft.AspNetCore.ResponseCompression.csproj",
"StaticFiles\\samples\\StaticFileSample\\StaticFileSample.csproj",
"StaticFiles\\src\\Microsoft.AspNetCore.StaticFiles.csproj",
"StaticFiles\\test\\FunctionalTests\\Microsoft.AspNetCore.StaticFiles.FunctionalTests.csproj",
"StaticFiles\\test\\UnitTests\\Microsoft.AspNetCore.StaticFiles.Tests.csproj"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public void ConfigureServices(IServiceCollection services)

public void Configure(IApplicationBuilder app, IWebHostEnvironment host)
{
Console.WriteLine("webroot: " + host.WebRootPath);

app.UseResponseCompression();

app.UseFileServer(new FileServerOptions
Expand All @@ -37,6 +35,7 @@ public static void Main(string[] args)
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
// .UseHttpSys()
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.ResponseCompression" />
<Reference Include="Microsoft.AspNetCore.StaticFiles" />
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
<Reference Include="Microsoft.AspNetCore.Server.HttpSys" />
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
<Reference Include="Microsoft.Extensions.Logging.Console" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
A static HTML file.<br />
</body>
</html>
7 changes: 5 additions & 2 deletions src/Middleware/StaticFiles/src/DefaultFilesMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
Expand Down Expand Up @@ -81,8 +82,10 @@ public Task Invoke(HttpContext context)
// This prevents relative links from breaking.
if (!Helpers.PathEndsInSlash(context.Request.Path))
{
context.Response.StatusCode = 301;
context.Response.Headers[HeaderNames.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
context.Response.StatusCode = StatusCodes.Status301MovedPermanently;
var request = context.Request;
var redirect = UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path + "/", request.QueryString);
context.Response.Headers[HeaderNames.Location] = redirect;
return Task.CompletedTask;
}

Expand Down
7 changes: 5 additions & 2 deletions src/Middleware/StaticFiles/src/DirectoryBrowserMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
Expand Down Expand Up @@ -88,8 +89,10 @@ public Task Invoke(HttpContext context)
// This prevents relative links from breaking.
if (!Helpers.PathEndsInSlash(context.Request.Path))
{
context.Response.StatusCode = 301;
context.Response.Headers[HeaderNames.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
context.Response.StatusCode = StatusCodes.Status301MovedPermanently;
var request = context.Request;
var redirect = UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path + "/", request.QueryString);
context.Response.Headers[HeaderNames.Location] = redirect;
return Task.CompletedTask;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,8 @@ private async Task NearMatch_RedirectAddSlash(string baseUrl, string baseDir, st

Assert.Equal(HttpStatusCode.Moved, response.StatusCode);
// 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.Equal("http://localhost" + baseUrl + new PathString(requestUrl + "/") + queryString, actualURL);
Assert.Empty((await response.Content.ReadAsByteArrayAsync()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private async Task NearMatch_RedirectAddSlash(string baseUrl, string baseDir, st
var response = await server.CreateRequest(requestUrl + queryString).GetAsync();

Assert.Equal(HttpStatusCode.Moved, response.StatusCode);
Assert.Equal(requestUrl + "/" + queryString, response.Headers.GetValues("Location").FirstOrDefault());
Assert.Equal("http://localhost" + requestUrl + "/" + queryString, response.Headers.GetValues("Location").FirstOrDefault());
Assert.Empty((await response.Content.ReadAsByteArrayAsync()));
}
}
Expand Down