Ejemplo n.º 1
0
        public string GetRightPart(UriPartial uriPartial)
        {
            string leftPart  = GetLeftPart(uriPartial);
            string rightPart = AbsoluteUri.Substring(leftPart.Length);

            return(rightPart);
        }
Ejemplo n.º 2
0
        private void Parse()
        {
            if (Scheme.ToLowerInvariant() != "data")
            {
                throw new FormatException("URI scheme must be 'data'.");
            }

            int dataDelIndex = AbsoluteUri.IndexOf(',');

            if (dataDelIndex == -1)
            {
                throw new FormatException("Not a valid data URI. Missing ','.");
            }

            string mediaTypeValueString = AbsoluteUri.Substring(5, dataDelIndex - 5);

            if (mediaTypeValueString.EndsWith(";base64"))
            {
                mediaTypeValueString = mediaTypeValueString.Remove(mediaType.Length - 7);
                data = Convert.FromBase64String(AbsoluteUri.Remove(0, dataDelIndex + 1));
            }
            else
            {
                Regex    dataRegEx = new Regex("((%[A-Fa-f0-9]{2})|.)*");
                string[] octets    = dataRegEx.Split(AbsoluteUri.Remove(0, dataDelIndex + 1));
                using (MemoryStream ms = new MemoryStream(octets.Length))
                {
                    foreach (string octet in octets)
                    {
                        if (octet.StartsWith("%"))
                        {
                            ms.WriteByte(byte.Parse(octet.Substring(1), NumberStyles.AllowHexSpecifier));
                        }
                        else
                        {
                            ms.WriteByte((byte)octet[0]);
                        }
                    }
                    data = ms.ToArray();
                }
            }

            this.mediaType = mediaTypeValueString;

            if (mediaTypeValueString == string.Empty)
            {
                mediaTypeValueString = "text/plain;charset=US-ASCII";
            }
            else if (mediaType.StartsWith(";") && HttpUtility.UrlDecode(mediaType).ToLowerInvariant().Contains(";charset="))
            {
                mediaTypeValueString = "text/plain" + mediaType;
            }

            string type = null, subtype = null;
            NameValueCollection parameters   = null;

            int mediaTypeSubTypeDividerIndex = mediaTypeValueString.IndexOf('/');
            int parameterStartIndex          = mediaTypeValueString.IndexOf(';');

            string[] mediaTypesSplit;
            string   parametersString = null;

            if (parameterStartIndex == -1)
            {
                mediaTypesSplit = mediaTypeValueString.Split(new char[] { '/' });
            }
            else
            {
                mediaTypesSplit  = mediaTypeValueString.Remove(parameterStartIndex).Split(new char[] { '/' });
                parametersString = mediaTypeValueString.Substring(parameterStartIndex);
            }

            type    = HttpUtility.UrlDecode(mediaTypesSplit[0]);
            subtype = HttpUtility.UrlDecode(mediaTypesSplit[1]);

            Regex token = new Regex(@"^[^\x00-\x20\x7F()<>@,;:\\""/[\]\?=]+$");

            if (mediaTypesSplit.Length != 2 || !token.IsMatch(type) || !token.IsMatch(subtype))
            {
                throw new FormatException("MediaType is not valid.");
            }

            if (!string.IsNullOrEmpty(parametersString))
            {
                parameters = new NameValueCollection();
                using (StringReader reader = new StringReader(parametersString))
                {
                    int  value = reader.Read();
                    char c     = (char)value;
                    do
                    {
                        if (c != ';')
                        {
                            throw new FormatException("MediaType is not valid.");
                        }

                        StringBuilder attributeBuilder = new StringBuilder();
                        while ((value = reader.Read()) != -1 && (c = (char)value) != '=')
                        {
                            attributeBuilder.Append(c);
                        }

                        string attribute = HttpUtility.UrlDecode(attributeBuilder.ToString());
                        if (c != '=' || !token.IsMatch(attribute))
                        {
                            throw new FormatException("MediaType is not valid.");
                        }

                        StringBuilder valueBuilder = new StringBuilder();
                        while ((value = reader.Read()) != -1 && (c = (char)value) != ';')
                        {
                            valueBuilder.Append(c);
                        }

                        string encodedParameterValue = valueBuilder.ToString();

                        string parameterValue = null;
                        if (Regex.IsMatch(encodedParameterValue, @"^(?:(?:%[A-Fa-f0-9]{2})|[^\x00-\x20\x7F()<>@,;:\\""/[\]\?=])*$"))
                        {
                            parameterValue = HttpUtility.UrlDecode(encodedParameterValue);
                        }
                        else if (encodedParameterValue.StartsWith("%22") && reader.Peek() != -1)
                        {
                            string encodedParametersEnd = reader.ReadToEnd();
                            string parametersEnd        = HttpUtility.UrlDecode(reader.ReadToEnd());

                            Match m = Regex.Match(parametersEnd, @"((?:^"")|(?:[^\\]""))[;$]");
                            if (!m.Success)
                            {
                                throw new FormatException("MediaType is not valid.");
                            }

                            parameterValue = Regex.Replace(HttpUtility.UrlDecode(encodedParameterValue.Remove(0, 3)) + parametersEnd.Remove(m.Index + m.Length - 1), @"\(.)", "$2");
                        }
                        else
                        {
                            throw new FormatException("MediaType is not valid.");
                        }

                        if (!Regex.IsMatch(parameterValue, @"[\x00-\x7F]*"))
                        {
                            throw new FormatException("MediaType is not valid.");
                        }

                        parameters.Add(attribute, parameterValue);
                    }while (value != -1);
                }
            }

            mediaTypeValue = new MediaType(type, subtype, parameters);
        }
Ejemplo n.º 3
0
        private new void Parse()
        {
            if (Scheme.ToLowerInvariant() != "data")
            {
                throw new FormatException("URI scheme must be 'data'.");
            }

            int dataDelIndex = AbsoluteUri.IndexOf(',');

            if (dataDelIndex == -1)
            {
                throw new FormatException("Not a valid data URI. Missing ','.");
            }

            string mediaType = AbsoluteUri.Substring(5, dataDelIndex - 5);

            if (mediaType.EndsWith(";base64"))
            {
                mediaType = mediaType.Remove(mediaType.Length - 7);
                data      = Convert.FromBase64String(AbsoluteUri.Remove(0, dataDelIndex + 1));
            }
            else
            {
                data = HttpUtility.UrlDecodeToBytes(AbsoluteUri.Remove(0, dataDelIndex + 1), Encoding.ASCII);
            }

            if (mediaType == string.Empty)
            {
                mediaType = "text/plain;charset=US-ASCII";
            }
            else if (mediaType.StartsWith(";") && HttpUtility.UrlDecode(mediaType).ToLowerInvariant().Contains(";charset="))
            {
                mediaType = "text/plain" + mediaType;
            }

            string type = null, subtype = null;
            NameValueCollection parameters = null;

            Regex mediaTypePattern         = new Regex("(?<type>[^/;]+)/(?<subtype>[^/;]+)(;(?<parameter>(?<attribute>[^=;]+)=(?<value>[^=;]+)))*");
            Match m = mediaTypePattern.Match(mediaType);

            if (!m.Success)
            {
                throw new FormatException("MediaType is not valid.");
            }

            type    = HttpUtility.UrlDecode(m.Groups["type"].Value);
            subtype = HttpUtility.UrlDecode(m.Groups["subtype"].Value);
            if (m.Groups["parameter"].Success)
            {
                parameters = new NameValueCollection();
                for (int i = 0; i < m.Groups["attribute"].Captures.Count; i++)
                {
                    string value = HttpUtility.UrlDecode(m.Groups["value"].Captures[i].Value);
                    if (value.StartsWith("\""))
                    {
                        value = value.Substring(1, value.Length - 2);
                    }
                    parameters.Add(HttpUtility.UrlDecode(m.Groups["attribute"].Captures[i].Value), HttpUtility.UrlDecode(m.Groups["value"].Captures[i].Value));
                }
            }

            this.mediaType = new MediaType(type, subtype, parameters);
        }