Esempio n. 1
0
        public static QueryParamCollection ParseQueryString(string query, Encoding encoding)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            if (query.Length == 0 || (query.Length == 1 && query[0] == '?'))
            {
                return(new QueryParamCollection());
            }
            if (query[0] == '?')
            {
                query = query.Substring(1);
            }

            QueryParamCollection result = new QueryParamCollection();

            ParseQueryString(query, encoding, result);
            return(result);
        }
Esempio n. 2
0
        internal static void ParseQueryString(string query, Encoding encoding, QueryParamCollection result)
        {
            if (query.Length == 0)
            {
                return;
            }

            string decoded       = HtmlDecode(query);
            int    decodedLength = decoded.Length;
            int    namePos       = 0;
            bool   first         = true;

            while (namePos <= decodedLength)
            {
                int valuePos = -1, valueEnd = -1;
                for (int q = namePos; q < decodedLength; q++)
                {
                    if (valuePos == -1 && decoded[q] == '=')
                    {
                        valuePos = q + 1;
                    }
                    else if (decoded[q] == '&')
                    {
                        valueEnd = q;
                        break;
                    }
                }

                if (first)
                {
                    first = false;
                    if (decoded[namePos] == '?')
                    {
                        namePos++;
                    }
                }

                string name, value;
                if (valuePos == -1)
                {
                    name     = null;
                    valuePos = namePos;
                }
                else
                {
                    name = UrlDecode(decoded.Substring(namePos, valuePos - namePos - 1), encoding);
                }
                if (valueEnd < 0)
                {
                    namePos  = -1;
                    valueEnd = decoded.Length;
                }
                else
                {
                    namePos = valueEnd + 1;
                }
                value = UrlDecode(decoded.Substring(valuePos, valueEnd - valuePos), encoding);

                result.Add(name, value);
                if (namePos == -1)
                {
                    break;
                }
            }
        }