示例#1
0
        public static ParameterCollection Parse(ITextParser reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            var parameters = new ParameterCollection();
            while (!reader.EndOfFile)
            {
                string name = Uri.UnescapeDataString(reader.ReadToEnd("&="));
                char current = reader.Current;
                reader.Consume();
                switch (current)
                {
                    case '&':
                        parameters.Add(name, string.Empty);
                        break;
                    case '=':
                        {
                            string value = reader.ReadToEnd("&");
                            reader.Consume();
                            parameters.Add(name, Uri.UnescapeDataString(value));
                        }
                        break;
                    default:
                        parameters.Add(name, string.Empty);
                        break;
                }
            }

            return parameters;
        }
示例#2
0
        /// <summary>
        /// Parse a header
        /// </summary>
        /// <param name="name">Name of header.</param>
        /// <param name="reader">Reader containing value.</param>
        /// <returns>HTTP Header</returns>
        /// <exception cref="FormatException">Header value is not of the expected format.</exception>
        public IHeader Parse(string name, ITextParser reader)
        {
            //key: "value"; key: "value"

            var cookies = new HttpCookieCollection();
            while (!reader.EndOfFile)
            {
                // read name
                string cookieName = reader.ReadToEnd("=;");

                // cookie with value?
                if (reader.Current == '=')
                {
                    reader.Consume();
                    reader.ConsumeWhiteSpaces();

                    // is value quoted or not?
                    string value = reader.Current == '"' ? reader.ReadQuotedString() : reader.ReadToEnd(";");
                    cookies.Add(new HttpCookie(cookieName, value));
                }
                //else
                //    cookies.Add(new RequestCookie(cookieName, string.Empty));

                // consume whitespaces and cookie separator
                reader.ConsumeWhiteSpaces(';');
            }

            return new CookieHeader(cookies);
        }