Throw if UseUrls specifies HTTPS or path base (#1519).

This commit is contained in:
Cesar Blum Silveira 2017-03-29 20:23:51 -07:00 committed by GitHub
parent e64bffd25c
commit 1be31ae2ce
4 changed files with 47 additions and 65 deletions

View File

@ -146,6 +146,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel
{
var parsedAddress = ServerAddress.FromUrl(address);
if (parsedAddress.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException($"HTTPS endpoints can only be configured using {nameof(KestrelServerOptions)}.{nameof(KestrelServerOptions.Listen)}().");
}
if (!string.IsNullOrEmpty(parsedAddress.PathBase))
{
throw new InvalidOperationException($"A path base can only be configured using {nameof(IApplicationBuilder)}.UsePathBase().");
}
if (!string.IsNullOrEmpty(parsedAddress.PathBase))
{
_logger.LogWarning($"Path base in address {address} is not supported and will be ignored. To specify a path base, use {nameof(IApplicationBuilder)}.UsePathBase().");

View File

@ -289,9 +289,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
dataset.Add($"http://*:{port}/", _ => new[] { $"http://127.0.0.1:{port}/" });
dataset.Add($"http://+:{port}/", _ => new[] { $"http://127.0.0.1:{port}/" });
// Path after port
dataset.Add($"http://127.0.0.1:{port}/base/path", _ => new[] { $"http://127.0.0.1:{port}/base/path" });
// Dynamic port and non-loopback addresses
dataset.Add("http://127.0.0.1:0/", GetTestUrls);
dataset.Add($"http://{Dns.GetHostName()}:0/", GetTestUrls);
@ -411,10 +408,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
dataset.Add($"http://127.0.0.1:{port}/;http://[::1]:{port}/",
_ => new[] { $"http://127.0.0.1:{port}/", $"http://[::1]:{port}/" });
// Path after port
dataset.Add($"http://[::1]:{port}/base/path",
_ => new[] { $"http://[::1]:{port}/base/path" });
// Dynamic port and non-loopback addresses
var ipv6Addresses = GetIPAddresses()
.Where(ip => ip.AddressFamily == AddressFamily.InterNetworkV6)

View File

@ -566,64 +566,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
}
}
[Theory]
[InlineData("/base", "/base")]
[InlineData("/base", "/base/")]
[InlineData("/base", "/base/something")]
[InlineData("/base", "/base/something/")]
[InlineData("/base/something", "/base/something")]
[InlineData("/base/something", "/base/something/")]
[InlineData("/base/something", "/base/something/more")]
[InlineData("/base/something", "/base/something/more/")]
public async Task DoesNotSplitPathBase(string registerPathBase, string requestPath)
{
var testLogger = new TestApplicationErrorLogger();
string contextPathBase = null;
string contextPath = null;
var builder = new WebHostBuilder()
.UseKestrel()
.UseUrls($"http://127.0.0.1:0{registerPathBase}")
.ConfigureServices(services =>
{
services.AddSingleton<ILoggerFactory>(new KestrelTestLoggerFactory(testLogger));
})
.Configure(app =>
{
app.Run(context =>
{
contextPathBase = context.Request.PathBase;
contextPath = context.Request.Path;
return TaskCache.CompletedTask;
});
});
using (var host = builder.Build())
{
host.Start();
using (var connection = new TestConnection(host.GetPort()))
{
await connection.Send($"GET {requestPath} HTTP/1.1\r\n\r\n");
await connection.Receive("HTTP/1.1 200 OK");
}
Assert.Single(testLogger.Messages, log =>
log.LogLevel == LogLevel.Warning &&
string.Equals(
$"Path base in address http://127.0.0.1:0{registerPathBase} is not supported and will be ignored. To specify a path base, use {nameof(IApplicationBuilder)}.UsePathBase().",
log.Message,
StringComparison.Ordinal));
}
Assert.Equal("", contextPathBase);
Assert.Equal(requestPath, contextPath);
}
private async Task TestRemoteIPAddress(string registerAddress, string requestAddress, string expectAddress)
{
var builder = new WebHostBuilder()

View File

@ -14,6 +14,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
using Microsoft.AspNetCore.Builder;
namespace Microsoft.AspNetCore.Server.KestrelTests
{
@ -35,6 +36,42 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
}
}
[Fact]
public void StartWithHttpsAddressThrows()
{
var testLogger = new TestApplicationErrorLogger { ThrowOnCriticalErrors = false };
using (var server = CreateServer(new KestrelServerOptions(), testLogger))
{
server.Features.Get<IServerAddressesFeature>().Addresses.Add("https://127.0.0.1:0");
var exception = Assert.Throws<InvalidOperationException>(() => StartDummyApplication(server));
Assert.Equal(
$"HTTPS endpoints can only be configured using {nameof(KestrelServerOptions)}.{nameof(KestrelServerOptions.Listen)}().",
exception.Message);
Assert.Equal(1, testLogger.CriticalErrorsLogged);
}
}
[Fact]
public void StartWithPathBaseInAddressThrows()
{
var testLogger = new TestApplicationErrorLogger { ThrowOnCriticalErrors = false };
using (var server = CreateServer(new KestrelServerOptions(), testLogger))
{
server.Features.Get<IServerAddressesFeature>().Addresses.Add("http://127.0.0.1:0/base");
var exception = Assert.Throws<InvalidOperationException>(() => StartDummyApplication(server));
Assert.Equal(
$"A path base can only be configured using {nameof(IApplicationBuilder)}.UsePathBase().",
exception.Message);
Assert.Equal(1, testLogger.CriticalErrorsLogged);
}
}
[Theory]
[InlineData("http://localhost:5000")]
[InlineData("The value of the string shouldn't matter.")]