Exemplo n.º 1
0
        /// <summary>
        /// Returns an <see cref="IPEndPoint"/> for the given host an port.
        /// If the host parameter isn't "localhost" or an IP address, use IPAddress.Any.
        /// </summary>
        protected internal static bool TryCreateIPEndPoint(BindingAddress address, out IPEndPoint endpoint)
        {
            if (!IPAddress.TryParse(address.Host, out var ip))
            {
                endpoint = null;
                return(false);
            }

            endpoint = new IPEndPoint(ip, address.Port);
            return(true);
        }
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);
        }
Exemplo n.º 3
0
        public static BindingAddress Parse(string address)
        {
            address ??= string.Empty;

            var schemeDelimiterStart = address.IndexOf("://", StringComparison.Ordinal);

            if (schemeDelimiterStart < 0)
            {
                throw new FormatException($"Invalid url: '{address}'");
            }
            var schemeDelimiterEnd = schemeDelimiterStart + "://".Length;

            var isUnixPipe = address.IndexOf(UnixPipeHostPrefix, schemeDelimiterEnd, StringComparison.Ordinal) ==
                             schemeDelimiterEnd;

            int pathDelimiterStart;
            int pathDelimiterEnd;

            if (!isUnixPipe)
            {
                pathDelimiterStart = address.IndexOf("/", schemeDelimiterEnd, StringComparison.Ordinal);
                pathDelimiterEnd   = pathDelimiterStart;
            }
            else
            {
                pathDelimiterStart = address.IndexOf(":", schemeDelimiterEnd + UnixPipeHostPrefix.Length,
                                                     StringComparison.Ordinal);
                pathDelimiterEnd = pathDelimiterStart + ":".Length;
            }

            if (pathDelimiterStart < 0)
            {
                pathDelimiterStart = pathDelimiterEnd = address.Length;
            }

            var serverAddress = new BindingAddress {
                Scheme = address.Substring(0, schemeDelimiterStart)
            };

            var hasSpecifiedPort = false;

            if (!isUnixPipe)
            {
                var portDelimiterStart = address.LastIndexOf(":", pathDelimiterStart - 1,
                                                             pathDelimiterStart - schemeDelimiterEnd, StringComparison.Ordinal);
                if (portDelimiterStart >= 0)
                {
                    var portDelimiterEnd = portDelimiterStart + ":".Length;

                    var portString = address.Substring(portDelimiterEnd, pathDelimiterStart - portDelimiterEnd);
                    if (int.TryParse(portString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var portNumber))
                    {
                        hasSpecifiedPort   = true;
                        serverAddress.Host =
                            address.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 = address.Substring(schemeDelimiterEnd, pathDelimiterStart - schemeDelimiterEnd);
            }

            if (string.IsNullOrEmpty(serverAddress.Host))
            {
                throw new FormatException($"Invalid url: '{address}'");
            }

            serverAddress.PathBase = address[^ 1] == '/' ? address.Substring(pathDelimiterEnd, address.Length - pathDelimiterEnd - 1) : address.Substring(pathDelimiterEnd);