diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs index 02ac364178..2b8678b5ff 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs @@ -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()."); diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs index 5a605a74c6..1abb18fe40 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs @@ -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) diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs index 10a718c1e8..6b9007c6d3 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs @@ -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(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() diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs index dd36a36669..2f103ff376 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs @@ -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().Addresses.Add("https://127.0.0.1:0"); + + var exception = Assert.Throws(() => 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().Addresses.Add("http://127.0.0.1:0/base"); + + var exception = Assert.Throws(() => 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.")]