예제 #1
0
        /// <summary>
        /// Splits a ampersand-separated list of key-value pairs, decodes the keys and
        /// values, and adds them to a NameValueCollection. Keys and values are separated
        /// by equals signs.
        /// </summary>
        /// <param name="input">The key-value pair list</param>
        /// <returns>A name value collection, which may be empty.</returns>
        /// <exception cref="System.FormatException">
        /// If the string is not a series of key-value pairs separated by ampersands,
        /// or if one of the keys is null or empty, or if one of the keys or values is
        /// not properly encoded.
        /// </exception>
        public static NameValueCollection SplitAndDecode(string input)
        {
            NameValueCollection parameters = new NameValueCollection();

            if (string.IsNullOrEmpty(input))
            {
                return(parameters);
            }

            foreach (string pair in input.Split('&'))
            {
                string[] parts = pair.Split('=');

                if (parts.Length != 2)
                {
                    throw new FormatException("Pair is not a key-value pair");
                }

                string key = Rfc3986.Decode(parts[0]);
                if (string.IsNullOrEmpty(key))
                {
                    throw new FormatException("Key cannot be null or empty");
                }

                string value = Rfc3986.Decode(parts[1]);

                parameters.Add(key, value);
            }

            return(parameters);
        }
예제 #2
0
        /**
         * Parse the parameters from an OAuth Authorization or WWW-Authenticate
         * header. The realm is included as a parameter. If the given header doesn't
         * start with "OAuth ", return an empty list.
         */
        public static List <OAuth.Parameter> decodeAuthorization(String authorization)
        {
            List <OAuth.Parameter> into = new List <OAuth.Parameter>();

            if (authorization != null)
            {
                Match m = AUTHORIZATION.Match(authorization);
                if (m.Success)
                {
                    if (m.Groups[1].Value.Equals(AUTH_SCHEME))
                    {
                        foreach (String nvp in Regex.Split(m.Groups[2].Value, "\\s*,\\s*"))
                        {
                            m = NVP.Match(nvp);
                            if (m.Success)
                            {
                                String name  = Rfc3986.Decode(m.Groups[1].Value);
                                String value = Rfc3986.Decode(m.Groups[2].Value);
                                into.Add(new OAuth.Parameter(name, value));
                            }
                        }
                    }
                }
            }
            return(into);
        }
예제 #3
0
        /** Parse a form-urlencoded document. */
        public static List <Parameter> decodeForm(String form)
        {
            List <Parameter> list = new List <Parameter>();

            if (!isEmpty(form))
            {
                foreach (String nvp in form.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    int    equals = nvp.IndexOf('=');
                    String name;
                    String value;
                    if (equals < 0)
                    {
                        name  = Rfc3986.Decode(nvp);
                        value = null;
                    }
                    else
                    {
                        name  = Rfc3986.Decode(nvp.Substring(0, equals));
                        value = Rfc3986.Decode(nvp.Substring(equals + 1));
                    }
                    list.Add(new Parameter(name, value));
                }
            }
            return(list);
        }