Beispiel #1
0
 protected void Parse()
 {
     this.Parse(this.source);
     if (this.userEscaped)
     {
         return;
     }
     this.host = Uri.EscapeString(this.host, false, true, false);
     this.path = Uri.EscapeString(this.path);
 }
Beispiel #2
0
 protected static string EscapeString(string str)
 {
     return(Uri.EscapeString(str, false, true, true));
 }
Beispiel #3
0
        public Uri(Uri baseUri, string relativeUri, bool dontEscape)
        {
            this.scheme   = string.Empty;
            this.host     = string.Empty;
            this.port     = -1;
            this.path     = string.Empty;
            this.query    = string.Empty;
            this.fragment = string.Empty;
            this.userinfo = string.Empty;
            this.reduce   = true;
            base..ctor();
            if (baseUri == null)
            {
                throw new NullReferenceException("baseUri");
            }
            this.userEscaped = dontEscape;
            if (relativeUri == null)
            {
                throw new NullReferenceException("relativeUri");
            }
            if (relativeUri.StartsWith("\\\\"))
            {
                this.source = relativeUri;
                this.Parse();
                return;
            }
            int num = relativeUri.IndexOf(':');

            if (num != -1)
            {
                int num2 = relativeUri.IndexOfAny(new char[]
                {
                    '/',
                    '\\',
                    '?'
                });
                if (num2 > num || num2 < 0)
                {
                    this.source = relativeUri;
                    this.Parse();
                    return;
                }
            }
            this.scheme         = baseUri.scheme;
            this.host           = baseUri.host;
            this.port           = baseUri.port;
            this.userinfo       = baseUri.userinfo;
            this.isUnc          = baseUri.isUnc;
            this.isUnixFilePath = baseUri.isUnixFilePath;
            this.isOpaquePart   = baseUri.isOpaquePart;
            if (relativeUri == string.Empty)
            {
                this.path     = baseUri.path;
                this.query    = baseUri.query;
                this.fragment = baseUri.fragment;
                return;
            }
            num = relativeUri.IndexOf('#');
            if (num != -1)
            {
                this.fragment = relativeUri.Substring(num);
                relativeUri   = relativeUri.Substring(0, num);
            }
            num = relativeUri.IndexOf('?');
            if (num != -1)
            {
                this.query = relativeUri.Substring(num);
                if (!this.userEscaped)
                {
                    this.query = Uri.EscapeString(this.query);
                }
                relativeUri = relativeUri.Substring(0, num);
            }
            if (relativeUri.Length > 0 && relativeUri[0] == '/')
            {
                if (relativeUri.Length > 1 && relativeUri[1] == '/')
                {
                    this.source = this.scheme + ':' + relativeUri;
                    this.Parse();
                    return;
                }
                this.path = relativeUri;
                if (!this.userEscaped)
                {
                    this.path = Uri.EscapeString(this.path);
                }
                return;
            }
            else
            {
                this.path = baseUri.path;
                if (relativeUri.Length > 0 || this.query.Length > 0)
                {
                    num = this.path.LastIndexOf('/');
                    if (num >= 0)
                    {
                        this.path = this.path.Substring(0, num + 1);
                    }
                }
                if (relativeUri.Length == 0)
                {
                    return;
                }
                this.path += relativeUri;
                int startIndex = 0;
                for (;;)
                {
                    num = this.path.IndexOf("./", startIndex);
                    if (num == -1)
                    {
                        break;
                    }
                    if (num == 0)
                    {
                        this.path = this.path.Remove(0, 2);
                    }
                    else if (this.path[num - 1] != '.')
                    {
                        this.path = this.path.Remove(num, 2);
                    }
                    else
                    {
                        startIndex = num + 1;
                    }
                }
                if (this.path.Length > 1 && this.path[this.path.Length - 1] == '.' && this.path[this.path.Length - 2] == '/')
                {
                    this.path = this.path.Remove(this.path.Length - 1, 1);
                }
                startIndex = 0;
                for (;;)
                {
                    num = this.path.IndexOf("/../", startIndex);
                    if (num == -1)
                    {
                        break;
                    }
                    if (num == 0)
                    {
                        startIndex = 3;
                    }
                    else
                    {
                        int num3 = this.path.LastIndexOf('/', num - 1);
                        if (num3 == -1)
                        {
                            startIndex = num + 1;
                        }
                        else if (this.path.Substring(num3 + 1, num - num3 - 1) != "..")
                        {
                            this.path = this.path.Remove(num3 + 1, num - num3 + 3);
                        }
                        else
                        {
                            startIndex = num + 1;
                        }
                    }
                }
                if (this.path.Length > 3 && this.path.EndsWith("/.."))
                {
                    num = this.path.LastIndexOf('/', this.path.Length - 4);
                    if (num != -1 && this.path.Substring(num + 1, this.path.Length - num - 4) != "..")
                    {
                        this.path = this.path.Remove(num + 1, this.path.Length - num - 1);
                    }
                }
                if (!this.userEscaped)
                {
                    this.path = Uri.EscapeString(this.path);
                }
                return;
            }
        }
Beispiel #4
0
 protected void Escape()
 {
     this.path = Uri.EscapeString(this.path);
 }
Beispiel #5
0
        private void Parse(string uriString)
        {
            if (uriString == null)
            {
                throw new ArgumentNullException("uriString");
            }
            int length = uriString.Length;

            if (length <= 1)
            {
                throw new FormatException();
            }
            int num = uriString.IndexOf(':');

            if (num < 0)
            {
                if (uriString[0] == '/')
                {
                    this.ParseAsUnixAbsoluteFilePath(uriString);
                }
                else
                {
                    if (!uriString.StartsWith("\\\\"))
                    {
                        throw new FormatException("URI scheme was not recognized, nor input string is not recognized as an absolute file path.");
                    }
                    this.ParseAsWindowsUNC(uriString);
                }
                return;
            }
            if (num == 1)
            {
                if (!char.IsLetter(uriString[0]))
                {
                    throw new FormatException("URI scheme must start with alphabet character.");
                }
                this.ParseAsWindowsAbsoluteFilePath(uriString);
                return;
            }
            else
            {
                this.scheme = uriString.Substring(0, num).ToLower(CultureInfo.InvariantCulture);
                if (!char.IsLetter(this.scheme[0]))
                {
                    throw new FormatException("URI scheme must start with alphabet character.");
                }
                for (int i = 1; i < this.scheme.Length; i++)
                {
                    if (!char.IsLetterOrDigit(this.scheme, i))
                    {
                        switch (this.scheme[i])
                        {
                        case '+':
                        case '-':
                        case '.':
                            goto IL_132;
                        }
                        throw new FormatException("URI scheme must consist of one of alphabet, digits, '+', '-' or '.' character.");
                    }
                    IL_132 :;
                }
                uriString = uriString.Substring(num + 1);
                num       = uriString.IndexOf('#');
                if (!this.IsUnc && num != -1)
                {
                    this.fragment = uriString.Substring(num);
                    uriString     = uriString.Substring(0, num);
                }
                num = uriString.IndexOf('?');
                if (num != -1)
                {
                    this.query = uriString.Substring(num);
                    uriString  = uriString.Substring(0, num);
                    if (!this.userEscaped)
                    {
                        this.query = Uri.EscapeString(this.query);
                    }
                }
                bool flag = this.scheme == Uri.UriSchemeFile && uriString.StartsWith("///");
                if (uriString.StartsWith("//"))
                {
                    if (uriString.StartsWith("////"))
                    {
                        flag = false;
                    }
                    uriString = uriString.TrimStart(new char[]
                    {
                        '/'
                    });
                    if (uriString.Length > 1 && uriString[1] == ':')
                    {
                        flag = false;
                    }
                }
                else if (!Uri.IsPredefinedScheme(this.scheme))
                {
                    this.path         = uriString;
                    this.isOpaquePart = true;
                    return;
                }
                num = uriString.IndexOfAny(new char[]
                {
                    '/'
                });
                if (flag)
                {
                    num = -1;
                }
                if (num == -1)
                {
                    if (this.scheme != Uri.UriSchemeMailto && this.scheme != Uri.UriSchemeNews && this.scheme != Uri.UriSchemeFile)
                    {
                        this.path = "/";
                    }
                }
                else
                {
                    this.path = uriString.Substring(num);
                    uriString = uriString.Substring(0, num);
                }
                num = uriString.IndexOf("@");
                if (num != -1)
                {
                    this.userinfo = uriString.Substring(0, num);
                    uriString     = uriString.Remove(0, num + 1);
                }
                this.port = -1;
                num       = uriString.LastIndexOf(":");
                if (flag)
                {
                    num = -1;
                }
                if (num != -1 && num != uriString.Length - 1)
                {
                    string text = uriString.Remove(0, num + 1);
                    if (text.Length > 1 && text[text.Length - 1] != ']')
                    {
                        try
                        {
                            this.port = (int)uint.Parse(text, CultureInfo.InvariantCulture);
                            uriString = uriString.Substring(0, num);
                        }
                        catch (Exception)
                        {
                            throw new FormatException("Invalid URI: invalid port number");
                        }
                    }
                }
                if (this.port == -1)
                {
                    this.port = Uri.GetDefaultPort(this.scheme);
                }
                this.host = uriString;
                if (flag)
                {
                    this.path = '/' + uriString;
                    this.host = string.Empty;
                }
                else if (this.host.Length == 2 && this.host[1] == ':')
                {
                    this.path = this.host + this.path;
                    this.host = string.Empty;
                }
                else if (this.isUnixFilePath)
                {
                    uriString = "//" + uriString;
                    this.host = string.Empty;
                }
                else
                {
                    if (this.host.Length == 0)
                    {
                        throw new FormatException("Invalid URI: The hostname could not be parsed");
                    }
                    if (this.scheme == Uri.UriSchemeFile)
                    {
                        this.isUnc = true;
                    }
                }
                if (this.scheme != Uri.UriSchemeMailto && this.scheme != Uri.UriSchemeNews && this.scheme != Uri.UriSchemeFile && this.reduce)
                {
                    this.path = Uri.Reduce(this.path);
                }
                return;
            }
        }