Novell.Directory.Ldap.LdapUrl.parseURL C# (CSharp) Method

parseURL() private method

private parseURL ( System url ) : void
url System
return void
        private void parseURL(System.String url)
        {
            int scanStart = 0;
            int scanEnd = url.Length;

            if ((System.Object) url == null)
                throw new System.UriFormatException("LdapUrl: URL cannot be null");

            // Check if URL is enclosed by < & >
            if (url[scanStart] == '<')
            {
                if (url[scanEnd - 1] != '>')
                    throw new System.UriFormatException("LdapUrl: URL bad enclosure");
                scanStart += 1;
                scanEnd -= 1;
            }

            // Determine the URL scheme and set appropriate default port
            if (url.Substring(scanStart, (scanStart + 4) - (scanStart)).ToUpper().Equals("URL:".ToUpper()))
            {
                scanStart += 4;
            }
            if (url.Substring(scanStart, (scanStart + 7) - (scanStart)).ToUpper().Equals("ldap://".ToUpper()))
            {
                scanStart += 7;
                port = LdapConnection.DEFAULT_PORT;
            }
            else if (url.Substring(scanStart, (scanStart + 8) - (scanStart)).ToUpper().Equals("ldaps://".ToUpper()))
            {
                secure = true;
                scanStart += 8;
                port = LdapConnection.DEFAULT_SSL_PORT;
            }
            else
            {
                throw new System.UriFormatException("LdapUrl: URL scheme is not ldap");
            }

            // Find where host:port ends and dn begins
            int dnStart = url.IndexOf("/", scanStart);
            int hostPortEnd = scanEnd;
            bool novell = false;
            if (dnStart < 0)
            {
                /*
                * Kludge. check for ldap://111.222.333.444:389??cn=abc,o=company
                *
                * Check for broken Novell referral format.  The dn is in
                * the scope position, but the required slash is missing.
                * This is illegal syntax but we need to account for it.
                * Fortunately it can't be confused with anything real.
                */
                dnStart = url.IndexOf("?", scanStart);
                if (dnStart > 0)
                {
                    if (url[dnStart + 1] == '?')
                    {
                        hostPortEnd = dnStart;
                        dnStart += 1;
                        novell = true;
                    }
                    else
                    {
                        dnStart = - 1;
                    }
                }
            }
            else
            {
                hostPortEnd = dnStart;
            }
            // Check for IPV6 "[ipaddress]:port"
            int portStart;
            int hostEnd = hostPortEnd;
            if (url[scanStart] == '[')
            {
                hostEnd = url.IndexOf((System.Char) ']', scanStart + 1);
                if ((hostEnd >= hostPortEnd) || (hostEnd == - 1))
                {
                    throw new System.UriFormatException("LdapUrl: \"]\" is missing on IPV6 host name");
                }
                // Get host w/o the [ & ]
                host = url.Substring(scanStart + 1, (hostEnd) - (scanStart + 1));
                portStart = url.IndexOf(":", hostEnd);
                if ((portStart < hostPortEnd) && (portStart != - 1))
                {
                    // port is specified
                    port = System.Int32.Parse(url.Substring(portStart + 1, (hostPortEnd) - (portStart + 1)));
                }
                else
                {
                }
            }
            else
            {
                portStart = url.IndexOf(":", scanStart);
                // Isolate the host and port
                if ((portStart < 0) || (portStart > hostPortEnd))
                {
                    // no port is specified, we keep the default
                    host = url.Substring(scanStart, (hostPortEnd) - (scanStart));
                }
                else
                {
                    // port specified in URL
                    host = url.Substring(scanStart, (portStart) - (scanStart));
                    port = System.Int32.Parse(url.Substring(portStart + 1, (hostPortEnd) - (portStart + 1)));
                }
            }

            scanStart = hostPortEnd + 1;
            if ((scanStart >= scanEnd) || (dnStart < 0))
                return ;

            // Parse out the base dn
            scanStart = dnStart + 1;

            int attrsStart = url.IndexOf((System.Char) '?', scanStart);
            if (attrsStart < 0)
            {
                dn = url.Substring(scanStart, (scanEnd) - (scanStart));
            }
            else
            {
                dn = url.Substring(scanStart, (attrsStart) - (scanStart));
            }

            scanStart = attrsStart + 1;
            // Wierd novell syntax can have nothing beyond the dn
            if ((scanStart >= scanEnd) || (attrsStart < 0) || novell)
                return ;

            // Parse out the attributes
            int scopeStart = url.IndexOf((System.Char) '?', scanStart);
            if (scopeStart < 0)
                scopeStart = scanEnd - 1;
            attrs = parseList(url, ',', attrsStart + 1, scopeStart);

            scanStart = scopeStart + 1;
            if (scanStart >= scanEnd)
                return ;

            // Parse out the scope
            int filterStart = url.IndexOf((System.Char) '?', scanStart);
            System.String scopeStr;
            if (filterStart < 0)
            {
                scopeStr = url.Substring(scanStart, (scanEnd) - (scanStart));
            }
            else
            {
                scopeStr = url.Substring(scanStart, (filterStart) - (scanStart));
            }
            if (scopeStr.ToUpper().Equals("".ToUpper()))
            {
                scope = LdapConnection.SCOPE_BASE;
            }
            else if (scopeStr.ToUpper().Equals("base".ToUpper()))
            {
                scope = LdapConnection.SCOPE_BASE;
            }
            else if (scopeStr.ToUpper().Equals("one".ToUpper()))
            {
                scope = LdapConnection.SCOPE_ONE;
            }
            else if (scopeStr.ToUpper().Equals("sub".ToUpper()))
            {
                scope = LdapConnection.SCOPE_SUB;
            }
            else
            {
                throw new System.UriFormatException("LdapUrl: URL invalid scope");
            }

            scanStart = filterStart + 1;
            if ((scanStart >= scanEnd) || (filterStart < 0))
                return ;

            // Parse out the filter
            scanStart = filterStart + 1;

            System.String filterStr;
            int extStart = url.IndexOf((System.Char) '?', scanStart);
            if (extStart < 0)
            {
                filterStr = url.Substring(scanStart, (scanEnd) - (scanStart));
            }
            else
            {
                filterStr = url.Substring(scanStart, (extStart) - (scanStart));
            }

            if (!filterStr.Equals(""))
            {
                filter = filterStr; // Only modify if not the default filter
            }

            scanStart = extStart + 1;
            if ((scanStart >= scanEnd) || (extStart < 0))
                return ;

            // Parse out the extensions
            int end = url.IndexOf((System.Char) '?', scanStart);
            if (end > 0)
                throw new System.UriFormatException("LdapUrl: URL has too many ? fields");
            extensions = parseList(url, ',', scanStart, scanEnd);

            return ;
        }