Remove obsolete ServerAddress type Kestrel/#2988

This commit is contained in:
Chris R 2018-11-29 15:16:19 -08:00
parent 30c8e4b1b0
commit 2b8c3db3b0
3 changed files with 6 additions and 227 deletions

View File

@ -1,152 +0,0 @@
// 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.Diagnostics;
using System.Globalization;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
namespace Microsoft.AspNetCore.Server.Kestrel.Core
{
[Obsolete("This is obsolete and will be removed in a future version. See https://github.com/aspnet/KestrelHttpServer/issues/2230")]
public class ServerAddress
{
public string Host { get; private set; }
public string PathBase { get; private set; }
public int Port { get; internal set; }
public string Scheme { get; private set; }
public bool IsUnixPipe
{
get
{
return Host.StartsWith(Constants.UnixPipeHostPrefix, StringComparison.Ordinal);
}
}
public string UnixPipePath
{
get
{
Debug.Assert(IsUnixPipe);
return Host.Substring(Constants.UnixPipeHostPrefix.Length - 1);
}
}
public override string ToString()
{
if (IsUnixPipe)
{
return Scheme.ToLowerInvariant() + "://" + Host.ToLowerInvariant();
}
else
{
return Scheme.ToLowerInvariant() + "://" + Host.ToLowerInvariant() + ":" + Port.ToString(CultureInfo.InvariantCulture);
}
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as ServerAddress;
if (other == null)
{
return false;
}
return string.Equals(Scheme, other.Scheme, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Host, other.Host, StringComparison.OrdinalIgnoreCase)
&& Port == other.Port;
}
public static ServerAddress FromUrl(string url)
{
url = url ?? string.Empty;
int schemeDelimiterStart = url.IndexOf("://", StringComparison.Ordinal);
if (schemeDelimiterStart < 0)
{
throw new FormatException(CoreStrings.FormatInvalidUrl(url));
}
int schemeDelimiterEnd = schemeDelimiterStart + "://".Length;
var isUnixPipe = url.IndexOf(Constants.UnixPipeHostPrefix, schemeDelimiterEnd, StringComparison.Ordinal) == schemeDelimiterEnd;
int pathDelimiterStart;
int pathDelimiterEnd;
if (!isUnixPipe)
{
pathDelimiterStart = url.IndexOf("/", schemeDelimiterEnd, StringComparison.Ordinal);
pathDelimiterEnd = pathDelimiterStart;
}
else
{
pathDelimiterStart = url.IndexOf(":", schemeDelimiterEnd + Constants.UnixPipeHostPrefix.Length, StringComparison.Ordinal);
pathDelimiterEnd = pathDelimiterStart + ":".Length;
}
if (pathDelimiterStart < 0)
{
pathDelimiterStart = pathDelimiterEnd = url.Length;
}
var serverAddress = new ServerAddress();
serverAddress.Scheme = url.Substring(0, schemeDelimiterStart);
var hasSpecifiedPort = false;
if (!isUnixPipe)
{
int portDelimiterStart = url.LastIndexOf(":", pathDelimiterStart - 1, pathDelimiterStart - schemeDelimiterEnd, StringComparison.Ordinal);
if (portDelimiterStart >= 0)
{
int portDelimiterEnd = portDelimiterStart + ":".Length;
string portString = url.Substring(portDelimiterEnd, pathDelimiterStart - portDelimiterEnd);
if (int.TryParse(portString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var portNumber))
{
hasSpecifiedPort = true;
serverAddress.Host = url.Substring(schemeDelimiterEnd, portDelimiterStart - schemeDelimiterEnd);
serverAddress.Port = portNumber;
}
}
if (!hasSpecifiedPort)
{
if (string.Equals(serverAddress.Scheme, "http", StringComparison.OrdinalIgnoreCase))
{
serverAddress.Port = 80;
}
else if (string.Equals(serverAddress.Scheme, "https", StringComparison.OrdinalIgnoreCase))
{
serverAddress.Port = 443;
}
}
}
if (!hasSpecifiedPort)
{
serverAddress.Host = url.Substring(schemeDelimiterEnd, pathDelimiterStart - schemeDelimiterEnd);
}
if (string.IsNullOrEmpty(serverAddress.Host))
{
throw new FormatException(CoreStrings.FormatInvalidUrl(url));
}
if (url[url.Length - 1] == '/')
{
serverAddress.PathBase = url.Substring(pathDelimiterEnd, url.Length - pathDelimiterEnd - 1);
}
else
{
serverAddress.PathBase = url.Substring(pathDelimiterEnd);
}
return serverAddress;
}
}
}

View File

@ -0,0 +1,6 @@
[
{
"TypeId": "public class Microsoft.AspNetCore.Server.Kestrel.Core.ServerAddress",
"Kind": "Removal"
}
]

View File

@ -1,75 +0,0 @@
// 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 Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
{
public class ServerAddressTests
{
[Theory]
[InlineData("")]
[InlineData("5000")]
[InlineData("//noscheme")]
public void FromUriThrowsForUrlsWithoutSchemeDelimiter(string url)
{
#pragma warning disable CS0618 // Type or member is obsolete
Assert.Throws<FormatException>(() => ServerAddress.FromUrl(url));
#pragma warning restore CS0618 // Type or member is obsolete
}
[Theory]
[InlineData("://")]
[InlineData("://:5000")]
[InlineData("http://")]
[InlineData("http://:5000")]
[InlineData("http:///")]
[InlineData("http:///:5000")]
[InlineData("http:////")]
[InlineData("http:////:5000")]
public void FromUriThrowsForUrlsWithoutHost(string url)
{
#pragma warning disable CS0618 // Type or member is obsolete
Assert.Throws<FormatException>(() => ServerAddress.FromUrl(url));
#pragma warning restore CS0618 // Type or member is obsolete
}
[Theory]
[InlineData("://emptyscheme", "", "emptyscheme", 0, "", "://emptyscheme:0")]
[InlineData("http://+", "http", "+", 80, "", "http://+:80")]
[InlineData("http://*", "http", "*", 80, "", "http://*:80")]
[InlineData("http://localhost", "http", "localhost", 80, "", "http://localhost:80")]
[InlineData("http://www.example.com", "http", "www.example.com", 80, "", "http://www.example.com:80")]
[InlineData("https://www.example.com", "https", "www.example.com", 443, "", "https://www.example.com:443")]
[InlineData("http://www.example.com/", "http", "www.example.com", 80, "", "http://www.example.com:80")]
[InlineData("http://www.example.com/foo?bar=baz", "http", "www.example.com", 80, "/foo?bar=baz", "http://www.example.com:80")]
[InlineData("http://www.example.com:5000", "http", "www.example.com", 5000, "", null)]
[InlineData("https://www.example.com:5000", "https", "www.example.com", 5000, "", null)]
[InlineData("http://www.example.com:5000/", "http", "www.example.com", 5000, "", "http://www.example.com:5000")]
[InlineData("http://www.example.com:NOTAPORT", "http", "www.example.com:NOTAPORT", 80, "", "http://www.example.com:notaport:80")]
[InlineData("https://www.example.com:NOTAPORT", "https", "www.example.com:NOTAPORT", 443, "", "https://www.example.com:notaport:443")]
[InlineData("http://www.example.com:NOTAPORT/", "http", "www.example.com:NOTAPORT", 80, "", "http://www.example.com:notaport:80")]
[InlineData("http://foo:/tmp/kestrel-test.sock:5000/doesn't/matter", "http", "foo:", 80, "/tmp/kestrel-test.sock:5000/doesn't/matter", "http://foo::80")]
[InlineData("http://unix:foo/tmp/kestrel-test.sock", "http", "unix:foo", 80, "/tmp/kestrel-test.sock", "http://unix:foo:80")]
[InlineData("http://unix:5000/tmp/kestrel-test.sock", "http", "unix", 5000, "/tmp/kestrel-test.sock", "http://unix:5000")]
[InlineData("http://unix:/tmp/kestrel-test.sock", "http", "unix:/tmp/kestrel-test.sock", 0, "", null)]
[InlineData("https://unix:/tmp/kestrel-test.sock", "https", "unix:/tmp/kestrel-test.sock", 0, "", null)]
[InlineData("http://unix:/tmp/kestrel-test.sock:", "http", "unix:/tmp/kestrel-test.sock", 0, "", "http://unix:/tmp/kestrel-test.sock")]
[InlineData("http://unix:/tmp/kestrel-test.sock:/", "http", "unix:/tmp/kestrel-test.sock", 0, "", "http://unix:/tmp/kestrel-test.sock")]
[InlineData("http://unix:/tmp/kestrel-test.sock:5000/doesn't/matter", "http", "unix:/tmp/kestrel-test.sock", 0, "5000/doesn't/matter", "http://unix:/tmp/kestrel-test.sock")]
public void UrlsAreParsedCorrectly(string url, string scheme, string host, int port, string pathBase, string toString)
{
#pragma warning disable CS0618 // Type or member is obsolete
var serverAddress = ServerAddress.FromUrl(url);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.Equal(scheme, serverAddress.Scheme);
Assert.Equal(host, serverAddress.Host);
Assert.Equal(port, serverAddress.Port);
Assert.Equal(pathBase, serverAddress.PathBase);
Assert.Equal(toString ?? url, serverAddress.ToString());
}
}
}