Пример #1
0
        internal HostPortrange(String scheme, String str)
        {
            // Parse the host name.  A name has up to three components, the
            // hostname, a port number, or two numbers representing a port
            // range.   "www.sun.com:8080-9090" is a valid host name.

            // With IPv6 an address can be 2010:836B:4179::836B:4179
            // An IPv6 address needs to be enclose in []
            // For ex: [2010:836B:4179::836B:4179]:8080-9090
            // Refer to RFC 2732 for more information.

            // first separate string into two fields: hoststr, portstr
            String hoststr, portstr = null;

            this.Scheme = scheme;

            // check for IPv6 address
            if (str.CharAt(0) == '[')
            {
                Ipv6 = Literal_Renamed = true;
                int rb = str.IndexOf(']');
                if (rb != -1)
                {
                    hoststr = str.Substring(1, rb - 1);
                }
                else
                {
                    throw new IllegalArgumentException("invalid IPv6 address: " + str);
                }
                int sep = str.IndexOf(':', rb + 1);
                if (sep != -1 && str.Length() > sep)
                {
                    portstr = str.Substring(sep + 1);
                }
                // need to normalize hoststr now
                sbyte[] ip = IPAddressUtil.textToNumericFormatV6(hoststr);
                if (ip == null)
                {
                    throw new IllegalArgumentException("illegal IPv6 address");
                }
                StringBuilder sb        = new StringBuilder();
                Formatter     formatter = new Formatter(sb, Locale.US);
                formatter.format("%02x%02x:%02x%02x:%02x%02x:%02x" + "%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13], ip[14], ip[15]);
                Hostname_Renamed = sb.ToString();
            }
            else
            {
                // not IPv6 therefore ':' is the port separator

                int sep = str.IndexOf(':');
                if (sep != -1 && str.Length() > sep)
                {
                    hoststr = str.Substring(0, sep);
                    portstr = str.Substring(sep + 1);
                }
                else
                {
                    hoststr = sep == -1 ? str : str.Substring(0, sep);
                }
                // is this a domain wildcard specification?
                if (hoststr.LastIndexOf('*') > 0)
                {
                    throw new IllegalArgumentException("invalid host wildcard specification");
                }
                else if (hoststr.StartsWith("*"))
                {
                    Wildcard_Renamed = true;
                    if (hoststr.Equals("*"))
                    {
                        hoststr = "";
                    }
                    else if (hoststr.StartsWith("*."))
                    {
                        hoststr = ToLowerCase(hoststr.Substring(1));
                    }
                    else
                    {
                        throw new IllegalArgumentException("invalid host wildcard specification");
                    }
                }
                else
                {
                    // check if ipv4 (if rightmost label a number)
                    // The normal way to specify ipv4 is 4 decimal labels
                    // but actually three, two or single label formats valid also
                    // So, we recognise ipv4 by just testing the rightmost label
                    // being a number.
                    int lastdot = hoststr.LastIndexOf('.');
                    if (lastdot != -1 && (hoststr.Length() > 1))
                    {
                        bool ipv4 = true;

                        for (int i = lastdot + 1, len = hoststr.Length(); i < len; i++)
                        {
                            char c = hoststr.CharAt(i);
                            if (c < '0' || c > '9')
                            {
                                ipv4 = false;
                                break;
                            }
                        }
                        this.Ipv4 = this.Literal_Renamed = ipv4;
                        if (ipv4)
                        {
                            sbyte[] ip = IPAddressUtil.textToNumericFormatV4(hoststr);
                            if (ip == null)
                            {
                                throw new IllegalArgumentException("illegal IPv4 address");
                            }
                            StringBuilder sb        = new StringBuilder();
                            Formatter     formatter = new Formatter(sb, Locale.US);
                            formatter.format("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
                            hoststr = sb.ToString();
                        }
                        else
                        {
                            // regular domain name
                            hoststr = ToLowerCase(hoststr);
                        }
                    }
                }
                Hostname_Renamed = hoststr;
            }

            try
            {
                Portrange_Renamed = ParsePort(portstr);
            }
            catch (Exception)
            {
                throw new IllegalArgumentException("invalid port range: " + portstr);
            }
        }