diff --git a/samples/SampleApp/Microsoft.AspNet.Hosting.ini b/samples/SampleApp/Microsoft.AspNet.Hosting.ini deleted file mode 100644 index 882b9e0262..0000000000 --- a/samples/SampleApp/Microsoft.AspNet.Hosting.ini +++ /dev/null @@ -1,3 +0,0 @@ - -Server = Microsoft.AspNet.Server.Kestrel -Server.Urls = http://localhost:5000/ diff --git a/samples/SampleApp/project.json b/samples/SampleApp/project.json index 2e0dd6f9c3..b77f0a7dd2 100644 --- a/samples/SampleApp/project.json +++ b/samples/SampleApp/project.json @@ -15,7 +15,6 @@ "commands": { "run": "Microsoft.AspNet.Server.Kestrel", "run-socket": "Microsoft.AspNet.Server.Kestrel --server.urls http://unix:/tmp/kestrel-test.sock", - "kestrel": "Microsoft.AspNet.Server.Kestrel", - "web": "Microsoft.AspNet.Hosting" + "kestrel": "Microsoft.AspNet.Server.Kestrel" } } diff --git a/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs b/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs index a153525ea1..597c45a4d4 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs @@ -3,35 +3,27 @@ using System; using System.Collections.Generic; +using Microsoft.AspNet.Server.Features; using Microsoft.Framework.Configuration; namespace Microsoft.AspNet.Server.Kestrel { - public class KestrelServerInformation : IKestrelServerInformation + public class KestrelServerInformation : IKestrelServerInformation, IServerAddressesFeature { - public KestrelServerInformation() - { - Addresses = new List(); - } - - public IList Addresses { get; private set; } + public ICollection Addresses { get; } = new List(); public int ThreadCount { get; set; } public void Initialize(IConfiguration configuration) { - var urls = configuration["server.urls"]; - if (string.IsNullOrEmpty(urls)) - { - urls = "http://+:5000/"; - } + var urls = configuration["server.urls"] ?? string.Empty; foreach (var url in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { - var address = ServerAddress.FromUrl(url); - if (address != null) - { - Addresses.Add(address); - } + Addresses.Add(url); + } + if (Addresses.Count == 0) + { + Addresses.Add("http://+:5000/"); } } } diff --git a/src/Microsoft.AspNet.Server.Kestrel/ServerAddress.cs b/src/Microsoft.AspNet.Server.Kestrel/ServerAddress.cs index 44fcf94d5c..4599e0a061 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/ServerAddress.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/ServerAddress.cs @@ -14,6 +14,29 @@ namespace Microsoft.AspNet.Server.Kestrel public int Port { get; private set; } public string Scheme { get; private set; } + public override string ToString() + { + return Scheme.ToLowerInvariant() + "://" + Host.ToLowerInvariant() + ":" + Port.ToString(CultureInfo.InvariantCulture) + Path.ToLowerInvariant(); + } + + 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 + && string.Equals(Path, other.Path, StringComparison.OrdinalIgnoreCase); + } + public static ServerAddress FromUrl(string url) { url = url ?? string.Empty; @@ -21,6 +44,17 @@ namespace Microsoft.AspNet.Server.Kestrel int schemeDelimiterStart = url.IndexOf("://", StringComparison.Ordinal); if (schemeDelimiterStart < 0) { + int port; + if (int.TryParse(url, NumberStyles.None, CultureInfo.InvariantCulture, out port)) + { + return new ServerAddress() + { + Scheme = "http", + Host = "+", + Port = port, + Path = "/" + }; + } return null; } int schemeDelimiterEnd = schemeDelimiterStart + "://".Length; diff --git a/src/Microsoft.AspNet.Server.Kestrel/ServerFactory.cs b/src/Microsoft.AspNet.Server.Kestrel/ServerFactory.cs index d1025b13b8..73201020d4 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/ServerFactory.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/ServerFactory.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNet.Hosting.Server; using Microsoft.AspNet.Http.Features; +using Microsoft.AspNet.Server.Features; using Microsoft.Dnx.Runtime; using Microsoft.Framework.Configuration; using Microsoft.Framework.Logging; @@ -34,6 +35,7 @@ namespace Microsoft.AspNet.Server.Kestrel information.Initialize(configuration); var serverFeatures = new FeatureCollection(); serverFeatures.Set(information); + serverFeatures.Set(information); return serverFeatures; } @@ -63,18 +65,33 @@ namespace Microsoft.AspNet.Server.Kestrel } engine.Start(information.ThreadCount == 0 ? 1 : information.ThreadCount); + bool atLeastOneListener = false; foreach (var address in information.Addresses) { - disposables.Push(engine.CreateServer( - address.Scheme, - address.Host, - address.Port, - async frame => - { - var request = new ServerRequest(frame); - await application.Invoke(request.Features).ConfigureAwait(false); - })); + var parsedAddress = ServerAddress.FromUrl(address); + if (parsedAddress == null) + { + throw new FormatException("Unrecognized listening address: " + address); + } + else + { + atLeastOneListener = true; + disposables.Push(engine.CreateServer( + parsedAddress.Scheme, + parsedAddress.Host, + parsedAddress.Port, + async frame => + { + var request = new ServerRequest(frame); + await application.Invoke(request.Features).ConfigureAwait(false); + })); + } + } + + if (!atLeastOneListener) + { + throw new InvalidOperationException("No recognized listening addresses were configured."); } return disposer;