Exemplo n.º 1
0
 internal static async Task BindEndpointAsync(ListenOptions.ListenOptions endpoint, AddressBindContext context)
 {
     try {
         await context.CreateBinding(endpoint).ConfigureAwait(false);
     } catch (AddressInUseException ex) {
         throw new IOException($"Failed to bind to address {endpoint}: address already in use.", ex);
     }
     context.ListenOptions.Add(endpoint);
 }
Exemplo n.º 2
0
        internal static ListenOptions.ListenOptions ParseAddress(string address, out bool https)
        {
            var parsedAddress = BindingAddress.Parse(address);

            https = false;

            if (parsedAddress.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
            {
                https = true;
            }
            else if (!parsedAddress.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException($"Unrecognized scheme in server address '{address}'. Only 'http://' is supported.");
            }

            if (!string.IsNullOrEmpty(parsedAddress.PathBase))
            {
                throw new InvalidOperationException($"A path base can only be configured using {nameof(IApplicationBuilder)}.UsePathBase().");
            }

            ListenOptions.ListenOptions options = null;
            if (parsedAddress.IsUnixPipe)
            {
                options = new ListenOptions.ListenOptions(parsedAddress.UnixPipePath);
            }
            else if (string.Equals(parsedAddress.Host, "localhost", StringComparison.OrdinalIgnoreCase))
            {
                // "localhost" for both IPv4 and IPv6 can't be represented as an IPEndPoint.
                options = new LocalhostListenOptions(parsedAddress.Port);
            }
            else if (TryCreateIPEndPoint(parsedAddress, out var endpoint))
            {
                options = new ListenOptions.ListenOptions(endpoint);
            }
            else
            {
                // when address is 'http://hostname:port', 'http://*:port', or 'http://+:port'
                options = new AnyIPListenOptions(parsedAddress.Port);
            }

            return(options);
        }