Switch to UsePathBase instead of relying on Kestrel feature #236

This commit is contained in:
John Luo 2016-08-16 20:20:55 -07:00
parent e94db64619
commit 2f397d6f63
3 changed files with 61 additions and 8 deletions

View File

@ -4,22 +4,26 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Server.IISIntegration
{
internal class IISSetupFilter : IStartupFilter
{
private string _pairingToken;
private readonly string _pairingToken;
private readonly PathString _pathBase;
internal IISSetupFilter(string pairingToken)
internal IISSetupFilter(string pairingToken, PathString pathBase)
{
_pairingToken = pairingToken;
_pathBase = pathBase;
}
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return app =>
{
app.UsePathBase(_pathBase);
app.UseForwardedHeaders();
app.UseMiddleware<IISMiddleware>(_pairingToken);
next(app);

View File

@ -3,6 +3,7 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Server.IISIntegration;
using Microsoft.Extensions.DependencyInjection;
@ -35,14 +36,14 @@ namespace Microsoft.AspNetCore.Hosting
if (!string.IsNullOrEmpty(port) && !string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(pairingToken))
{
var address = "http://localhost:" + port + path;
var address = "http://localhost:" + port;
hostBuilder.CaptureStartupErrors(true);
hostBuilder.ConfigureServices(services =>
{
// Delay register the url so users don't accidently overwrite it.
hostBuilder.UseSetting(WebHostDefaults.ServerUrlsKey, address);
services.AddSingleton<IStartupFilter>(new IISSetupFilter(pairingToken));
services.AddSingleton<IStartupFilter>(new IISSetupFilter(pairingToken, new PathString(path)));
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;

View File

@ -1,7 +1,6 @@
// 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;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
@ -79,14 +78,64 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
.UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration();
.UseIISIntegration()
.Configure(app =>
{
app.Run(context => Task.FromResult(0));
});
Assert.Null(builder.GetSetting(WebHostDefaults.ServerUrlsKey));
// Adds a server and calls Build()
var server = new TestServer(builder);
Assert.Equal("http://localhost:12345/", builder.GetSetting(WebHostDefaults.ServerUrlsKey));
Assert.Equal("http://localhost:12345", builder.GetSetting(WebHostDefaults.ServerUrlsKey));
}
[Fact]
public void PathBaseHiddenFromServer()
{
var builder = new WebHostBuilder()
.UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/pathBase")
.UseIISIntegration()
.Configure(app =>
{
app.Run(context => Task.FromResult(0));
});
new TestServer(builder);
Assert.Equal("http://localhost:12345", builder.GetSetting(WebHostDefaults.ServerUrlsKey));
}
[Fact]
public async Task AddsUsePathBaseMiddlewareWhenPathBaseSpecified()
{
var requestPathBase = string.Empty;
var requestPath = string.Empty;
var builder = new WebHostBuilder()
.UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/pathbase")
.UseIISIntegration()
.Configure(app =>
{
app.Run(context =>
{
requestPathBase = context.Request.PathBase.Value;
requestPath = context.Request.Path.Value;
return Task.FromResult(0);
});
});
var server = new TestServer(builder);
var request = new HttpRequestMessage(HttpMethod.Get, "/PathBase/Path");
request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken");
var response = await server.CreateClient().SendAsync(request);
Assert.Equal("/PathBase", requestPathBase);
Assert.Equal("/Path", requestPath);
}
[Fact]
@ -119,7 +168,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
Assert.True(assertsExecuted);
}
[Fact]
public async Task DoesNotAddAuthenticationHandlerIfWindowsAuthDisabled()
{