ServerAddress.FromUrl() should throw for invalid url (#875)

This commit is contained in:
Mike Harder 2016-05-27 12:02:30 -07:00
parent 8df6fbc500
commit 2453047fe2
4 changed files with 58 additions and 65 deletions

View File

@ -107,12 +107,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel
foreach (var address in _serverAddresses.Addresses.ToArray()) foreach (var address in _serverAddresses.Addresses.ToArray())
{ {
var parsedAddress = ServerAddress.FromUrl(address); var parsedAddress = ServerAddress.FromUrl(address);
if (parsedAddress == null)
{
throw new FormatException("Unrecognized listening address: " + address);
}
else
{
atLeastOneListener = true; atLeastOneListener = true;
if (!parsedAddress.Host.Equals("localhost", StringComparison.OrdinalIgnoreCase)) if (!parsedAddress.Host.Equals("localhost", StringComparison.OrdinalIgnoreCase))
@ -182,7 +176,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel
_serverAddresses.Addresses.Remove(address); _serverAddresses.Addresses.Remove(address);
_serverAddresses.Addresses.Add(parsedAddress.ToString()); _serverAddresses.Addresses.Add(parsedAddress.ToString());
} }
}
if (!atLeastOneListener) if (!atLeastOneListener)
{ {

View File

@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
int schemeDelimiterStart = url.IndexOf("://", StringComparison.Ordinal); int schemeDelimiterStart = url.IndexOf("://", StringComparison.Ordinal);
if (schemeDelimiterStart < 0) if (schemeDelimiterStart < 0)
{ {
return null; throw new FormatException($"Invalid URL: {url}");
} }
int schemeDelimiterEnd = schemeDelimiterStart + "://".Length; int schemeDelimiterEnd = schemeDelimiterStart + "://".Length;

View File

@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
var exception = Assert.Throws<FormatException>(() => StartDummyApplication(server)); var exception = Assert.Throws<FormatException>(() => StartDummyApplication(server));
Assert.Contains("Unrecognized listening address", exception.Message); Assert.Contains("Invalid URL", exception.Message);
} }
[Fact] [Fact]

View File

@ -14,9 +14,9 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[InlineData("")] [InlineData("")]
[InlineData("5000")] [InlineData("5000")]
[InlineData("//noscheme")] [InlineData("//noscheme")]
public void FromUriReturnsNullForSchemelessUrls(string url) public void FromUriThrowsForSchemelessUrls(string url)
{ {
Assert.Null(ServerAddress.FromUrl(url)); Assert.Throws<FormatException>(() => ServerAddress.FromUrl(url));
} }
[Theory] [Theory]