Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Throw if UseUrls specifies HTTPS or path base (#1519).
Browse files Browse the repository at this point in the history
  • Loading branch information
Cesar Blum Silveira committed Mar 21, 2017
1 parent 5fad3c7 commit 859a7d6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 62 deletions.
7 changes: 6 additions & 1 deletion src/Microsoft.AspNetCore.Server.Kestrel/KestrelServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,14 @@ public void Start<TContext>(IHttpApplication<TContext> application)
{
var parsedAddress = ServerAddress.FromUrl(address);

if (parsedAddress.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException($"HTTPS addresses are not supported. Use {nameof(KestrelServerOptions)}.{nameof(KestrelServerOptions.Listen)}() to configure an HTTPS endpoint.");
}

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().");
_logger.LogWarning($"Addresses containing a path base are not supported. Use {nameof(IApplicationBuilder)}.UsePathBase() to configure a path base in your application.");
}

if (parsedAddress.IsUnixPipe)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -20,10 +19,8 @@
using Microsoft.AspNetCore.Testing;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
Expand Down Expand Up @@ -524,64 +521,6 @@ await connection.Receive($"HTTP/1.1 200 OK",
}
}

[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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel;
Expand Down Expand Up @@ -45,6 +46,42 @@ public void StartWithInvalidAddressThrows()
Assert.Equal(1, testLogger.CriticalErrorsLogged);
}

[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 addresses are not supported. Use {nameof(KestrelServerOptions)}.{nameof(KestrelServerOptions.Listen)}() to configure an HTTPS endpoint.",
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(
$"Addresses containing a path base are not supported. Use {nameof(IApplicationBuilder)}.UsePathBase() to configure a path base in your application.",
exception.Message);
Assert.Equal(1, testLogger.CriticalErrorsLogged);
}
}

[Theory]
[InlineData("http://localhost:5000")]
[InlineData("The value of the string shouldn't matter.")]
Expand Down

0 comments on commit 859a7d6

Please sign in to comment.