GetStringValue() public méthode

public GetStringValue ( Token token ) : string
token Token
Résultat string
        static bool TryParseElement(Lexer lexer, out NameValueHeaderValue parsedValue, out Token t)
        {
            parsedValue = null;

            t = lexer.Scan();
            if (t != Token.Type.Token)
            {
                return(false);
            }

            parsedValue = new NameValueHeaderValue()
            {
                Name = lexer.GetStringValue(t),
            };

            t = lexer.Scan();
            if (t == Token.Type.SeparatorEqual)
            {
                t = lexer.Scan();

                if (t == Token.Type.Token || t == Token.Type.QuotedString)
                {
                    parsedValue.value = lexer.GetStringValue(t);
                    t = lexer.Scan();
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #2
0
        static bool TryParseElement(Lexer lexer, out ProductHeaderValue parsedValue, out Token t)
        {
            parsedValue = null;

            t = lexer.Scan();
            if (t != Token.Type.Token)
            {
                return(false);
            }

            parsedValue      = new ProductHeaderValue();
            parsedValue.Name = lexer.GetStringValue(t);

            t = lexer.Scan();
            if (t == Token.Type.SeparatorSlash)
            {
                t = lexer.Scan();
                if (t != Token.Type.Token)
                {
                    return(false);
                }

                parsedValue.Version = lexer.GetStringValue(t);
                t = lexer.Scan();
            }

            return(true);
        }
        internal static Token?TryParseMediaType(Lexer lexer, out string media)
        {
            media = null;

            var token = lexer.Scan();

            if (token != Token.Type.Token)
            {
                return(null);
            }

            if (lexer.Scan() != Token.Type.SeparatorSlash)
            {
                return(null);
            }

            var token2 = lexer.Scan();

            if (token2 != Token.Type.Token)
            {
                return(null);
            }

            media = lexer.GetStringValue(token) + "/" + lexer.GetStringValue(token2);

            return(lexer.Scan());
        }
        static bool TryParseElement(Lexer lexer, out StringWithQualityHeaderValue parsedValue, out Token t)
        {
            parsedValue = null;
            t           = lexer.Scan();
            if (t != Token.Type.Token)
            {
                return(false);
            }

            var value = new StringWithQualityHeaderValue();

            value.Value = lexer.GetStringValue(t);

            t = lexer.Scan();
            if (t == Token.Type.SeparatorSemicolon)
            {
                t = lexer.Scan();
                if (t != Token.Type.Token)
                {
                    return(false);
                }

                var s = lexer.GetStringValue(t);
                if (s != "q" && s != "Q")
                {
                    return(false);
                }

                t = lexer.Scan();
                if (t != Token.Type.SeparatorEqual)
                {
                    return(false);
                }

                t = lexer.Scan();

                double d;
                if (!lexer.TryGetDoubleValue(t, out d))
                {
                    return(false);
                }

                if (d > 1)
                {
                    return(false);
                }

                value.Quality = d;

                t = lexer.Scan();
            }

            parsedValue = value;
            return(true);
        }
        public static bool TryParse(string input, out RangeConditionHeaderValue parsedValue)
        {
            parsedValue = null;

            var  lexer = new Lexer(input);
            var  t     = lexer.Scan();
            bool is_weak;

            if (t == Token.Type.Token)
            {
                if (lexer.GetStringValue(t) != "W")
                {
                    DateTimeOffset date;
                    if (!Lexer.TryGetDateValue(input, out date))
                    {
                        return(false);
                    }

                    parsedValue = new RangeConditionHeaderValue(date);
                    return(true);
                }

                if (lexer.PeekChar() != '/')
                {
                    return(false);
                }

                is_weak = true;
                lexer.EatChar();
                t = lexer.Scan();
            }
            else
            {
                is_weak = false;
            }

            if (t != Token.Type.QuotedString)
            {
                return(false);
            }

            if (lexer.Scan() != Token.Type.End)
            {
                return(false);
            }

            parsedValue = new RangeConditionHeaderValue(
                new EntityTagHeaderValue()
            {
                Tag    = lexer.GetStringValue(t),
                IsWeak = is_weak
            });

            return(true);
        }
Exemple #6
0
        static bool TryParseElement(Lexer lexer, out ProductInfoHeaderValue parsedValue)
        {
            string comment;

            parsedValue = null;
            Token t;

            if (lexer.ScanCommentOptional(out comment, out t))
            {
                if (comment == null)
                {
                    return(false);
                }

                parsedValue         = new ProductInfoHeaderValue();
                parsedValue.Comment = comment;
                return(true);
            }

            if (t == Token.Type.End)
            {
                return(true);
            }

            if (t != Token.Type.Token)
            {
                return(false);
            }

            var value = new ProductHeaderValue();

            value.Name = lexer.GetStringValue(t);

            var pos = lexer.Position;

            t = lexer.Scan();
            if (t == Token.Type.SeparatorSlash)
            {
                t = lexer.Scan();
                if (t != Token.Type.Token)
                {
                    return(false);
                }

                value.Version = lexer.GetStringValue(t);
            }
            else
            {
                lexer.Position = pos;
            }

            parsedValue = new ProductInfoHeaderValue(value);
            return(true);
        }
Exemple #7
0
        static bool TryParseCollection(Lexer lexer, out List <NameValueHeaderValue> result, Token.Type separator)
        {
            var list = new List <NameValueHeaderValue> ();

            result = null;

            Token t;

            do
            {
                var attr = lexer.Scan();
                if (attr != Token.Type.Token)
                {
                    return(false);
                }

                string value = null;

                t = lexer.Scan();
                if (t == Token.Type.SeparatorEqual)
                {
                    t = lexer.Scan();
                    if (t != Token.Type.Token && t != Token.Type.QuotedString)
                    {
                        return(false);
                    }

                    value = lexer.GetStringValue(t);

                    t = lexer.Scan();
                }

                if (t == separator || t == Token.Type.End)
                {
                    list.Add(new NameValueHeaderValue()
                    {
                        Name  = lexer.GetStringValue(attr),
                        value = value
                    });
                }
                else
                {
                    return(false);
                }
            } while (t == separator);

            result = list;
            return(true);
        }
        static bool TryParseElement(Lexer lexer, out TransferCodingWithQualityHeaderValue parsedValue, out Token t)
        {
            parsedValue = null;

            t = lexer.Scan();
            if (t != Token.Type.Token)
            {
                return(false);
            }

            var result = new TransferCodingWithQualityHeaderValue();

            result.value = lexer.GetStringValue(t);

            t = lexer.Scan();

            // Parameters parsing
            if (t == Token.Type.SeparatorSemicolon && (!NameValueHeaderValue.TryParseParameters(lexer, out result.parameters, out t) || t != Token.Type.End))
            {
                return(false);
            }

            parsedValue = result;
            return(true);
        }
        internal static bool TryParse <T> (string input, out T parsedValue, Func <T> factory) where T : TransferCodingHeaderValue
        {
            parsedValue = null;

            var lexer = new Lexer(input);
            var t     = lexer.Scan();

            if (t != Token.Type.Token)
            {
                return(false);
            }

            var result = factory();

            result.value = lexer.GetStringValue(t);

            t = lexer.Scan();

            // Parameters parsing
            if (t == Token.Type.SeparatorSemicolon)
            {
                if (!NameValueHeaderValue.TryParseParameters(lexer, out result.parameters))
                {
                    return(false);
                }
            }
            else if (t != Token.Type.End)
            {
                return(false);
            }

            parsedValue = result;
            return(true);
        }
        internal static bool TryParseParameters(Lexer lexer, out List <NameValueHeaderValue> result, out Token t)
        {
            var list = new List <NameValueHeaderValue> ();

            result = null;

            while (true)
            {
                var attr = lexer.Scan();
                if (attr != Token.Type.Token)
                {
                    t = Token.Empty;
                    return(false);
                }

                string value = null;

                t = lexer.Scan();
                if (t == Token.Type.SeparatorEqual)
                {
                    t = lexer.Scan();
                    if (t != Token.Type.Token && t != Token.Type.QuotedString)
                    {
                        return(false);
                    }

                    value = lexer.GetStringValue(t);

                    t = lexer.Scan();
                }

                list.Add(new NameValueHeaderValue()
                {
                    Name  = lexer.GetStringValue(attr),
                    value = value
                });

                if (t == Token.Type.SeparatorSemicolon)
                {
                    continue;
                }

                result = list;
                return(true);
            }
        }
Exemple #11
0
        public static bool TryParse(string input, out NameValueHeaderValue parsedValue)
        {
            parsedValue = null;

            var lexer = new Lexer(input);
            var t     = lexer.Scan();

            if (t != Token.Type.Token && t != Token.Type.QuotedString)
            {
                return(false);
            }

            string v      = null;
            var    token2 = lexer.Scan();

            if (token2 != Token.Type.End)
            {
                if (token2 != Token.Type.SeparatorEqual)
                {
                    return(false);
                }

                token2 = lexer.Scan();

                if (token2 == Token.Type.Token || token2 == Token.Type.QuotedString)
                {
                    v      = lexer.GetStringValue(token2);
                    token2 = lexer.Scan();
                }

                if (token2 != Token.Type.End)
                {
                    return(false);
                }
            }

            parsedValue = new NameValueHeaderValue()
            {
                Name  = lexer.GetStringValue(t),
                value = v
            };

            return(true);
        }
Exemple #12
0
        static bool TryParseElement(Lexer lexer, out NameValueWithParametersHeaderValue parsedValue, out Token t)
        {
            parsedValue = null;

            t = lexer.Scan();
            if (t != Token.Type.Token)
            {
                return(false);
            }

            parsedValue = new NameValueWithParametersHeaderValue()
            {
                Name = lexer.GetStringValue(t),
            };

            t = lexer.Scan();
            if (t == Token.Type.SeparatorEqual)
            {
                t = lexer.Scan();

                if (t == Token.Type.Token || t == Token.Type.QuotedString)
                {
                    parsedValue.value = lexer.GetStringValue(t);
                    t = lexer.Scan();
                }
                else
                {
                    return(false);
                }
            }

            if (t == Token.Type.SeparatorSemicolon)
            {
                List <NameValueHeaderValue> result;
                if (!TryParseParameters(lexer, out result, out t))
                {
                    return(false);
                }

                parsedValue.parameters = result;
            }

            return(true);
        }
        static bool TryParseElement(Lexer lexer, out EntityTagHeaderValue parsedValue, out Token t)
        {
            parsedValue = null;

            t = lexer.Scan();
            bool is_weak = false;

            if (t == Token.Type.Token)
            {
                var s = lexer.GetStringValue(t);
                if (s == "*")
                {
                    parsedValue = any;

                    t = lexer.Scan();
                    return(true);
                }

                if (s != "W" || lexer.PeekChar() != '/')
                {
                    return(false);
                }

                is_weak = true;
                lexer.EatChar();
                t = lexer.Scan();
            }

            if (t != Token.Type.QuotedString)
            {
                return(false);
            }

            parsedValue        = new EntityTagHeaderValue();
            parsedValue.Tag    = lexer.GetStringValue(t);
            parsedValue.IsWeak = is_weak;

            t = lexer.Scan();

            return(true);
        }
Exemple #14
0
        static bool TryParseStringElement(Lexer lexer, out string parsedValue, out Token t)
        {
            t = lexer.Scan();
            if (t == Token.Type.Token)
            {
                parsedValue = lexer.GetStringValue(t);
                if (parsedValue.Length == 0)
                {
                    parsedValue = null;
                }

                t = lexer.Scan();
            }
            else
            {
                parsedValue = null;
            }

            return(true);
        }
        public static bool TryParse(string input, out ContentDispositionHeaderValue parsedValue)
        {
            parsedValue = null;

            var lexer = new Lexer(input);
            var t     = lexer.Scan();

            if (t.Kind != Token.Type.Token)
            {
                return(false);
            }

            List <NameValueHeaderValue> parameters = null;
            var type = lexer.GetStringValue(t);

            t = lexer.Scan();

            switch (t.Kind)
            {
            case Token.Type.SeparatorSemicolon:
                if (!NameValueHeaderValue.TryParseParameters(lexer, out parameters, out t) || t != Token.Type.End)
                {
                    return(false);
                }
                break;

            case Token.Type.End:
                break;

            default:
                return(false);
            }

            parsedValue = new ContentDispositionHeaderValue()
            {
                dispositionType = type,
                parameters      = parameters
            };

            return(true);
        }
        static bool TryParseElement(Lexer lexer, out AuthenticationHeaderValue parsedValue, out Token t)
        {
            t = lexer.Scan();
            if (t != Token.Type.Token)
            {
                parsedValue = null;
                return(false);
            }

            parsedValue        = new AuthenticationHeaderValue();
            parsedValue.Scheme = lexer.GetStringValue(t);

            t = lexer.Scan();
            if (t == Token.Type.Token)
            {
                // TODO: Wrong with multi value parsing
                parsedValue.Parameter = lexer.GetRemainingStringValue(t.StartPosition);
                t = new Token(Token.Type.End, 0, 0);
            }

            return(true);
        }
		public static bool TryParse (string input, out StringWithQualityHeaderValue parsedValue)
		{
			parsedValue = null;

			var lexer = new Lexer (input);
			var t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			var value = new StringWithQualityHeaderValue ();
			value.Value = lexer.GetStringValue (t);

			t = lexer.Scan ();
			if (t == Token.Type.SeparatorSemicolon) {
				t = lexer.Scan ();
				if (t != Token.Type.Token)
					return false;

				var s = lexer.GetStringValue (t);
				if (s != "q" && s != "Q")
					return false;

				t = lexer.Scan ();
				if (t != Token.Type.SeparatorEqual)
					return false;

				t = lexer.Scan ();

				double d;
				if (!lexer.TryGetDoubleValue (t, out d))
					return false;

				if (d > 1)
					return false;

				value.Quality = d;

				t = lexer.Scan ();
			}

			if (t != Token.Type.End)
				return false;

			parsedValue = value;
			return true;
		}
Exemple #18
0
        public static bool TryParse(string input, out WarningHeaderValue parsedValue)
        {
            parsedValue = null;

            var lexer = new Lexer(input);
            var t     = lexer.Scan();

            if (t != Token.Type.Token)
            {
                return(false);
            }

            int code;

            if (!lexer.TryGetNumericValue(t, out code) || !IsCodeValid(code))
            {
                return(false);
            }

            t = lexer.Scan();
            if (t != Token.Type.Token)
            {
                return(false);
            }

            var next = t;

            if (lexer.PeekChar() == ':')
            {
                lexer.EatChar();

                next = lexer.Scan();
                if (next != Token.Type.Token)
                {
                    return(false);
                }
            }

            var value = new WarningHeaderValue();

            value.Code  = code;
            value.Agent = lexer.GetStringValue(t, next);

            t = lexer.Scan();
            if (t != Token.Type.QuotedString)
            {
                return(false);
            }

            value.Text = lexer.GetStringValue(t);

            t = lexer.Scan();
            if (t == Token.Type.QuotedString)
            {
                DateTimeOffset date;
                if (!lexer.TryGetDateValue(t, out date))
                {
                    return(false);
                }

                value.Date = date;
                t          = lexer.Scan();
            }

            if (t != Token.Type.End)
            {
                return(false);
            }

            parsedValue = value;
            return(true);
        }
		static bool TryParseElement (Lexer lexer, out ProductInfoHeaderValue parsedValue)
		{
			string comment;
			parsedValue = null;
			Token t;

			if (lexer.ScanCommentOptional (out comment, out t)) {
				if (comment == null)
					return false;

				parsedValue = new ProductInfoHeaderValue ();
				parsedValue.Comment = comment;
				return true;
			}

			if (t == Token.Type.End)
				return true;

			if (t != Token.Type.Token)
				return false;

			var value = new ProductHeaderValue ();
			value.Name = lexer.GetStringValue (t);

			var pos = lexer.Position;
			t = lexer.Scan ();
			if (t == Token.Type.SeparatorSlash) {

				t = lexer.Scan ();
				if (t != Token.Type.Token)
					return false;

				value.Version = lexer.GetStringValue (t);
			} else {
				lexer.Position = pos;
			}

			parsedValue = new ProductInfoHeaderValue (value);
			return true;
		}
        public static bool TryParse(string input, out ContentRangeHeaderValue parsedValue)
        {
            parsedValue = null;

            var lexer = new Lexer(input);
            var t     = lexer.Scan();

            if (t != Token.Type.Token)
            {
                return(false);
            }

            var value = new ContentRangeHeaderValue();

            value.unit = lexer.GetStringValue(t);

            t = lexer.Scan();
            if (t != Token.Type.Token)
            {
                return(false);
            }

            int nvalue;

            if (!lexer.IsStarStringValue(t))
            {
                if (!lexer.TryGetNumericValue(t, out nvalue))
                {
                    var s = lexer.GetStringValue(t);
                    if (s.Length < 3)
                    {
                        return(false);
                    }

                    var sep = s.Split('-');
                    if (sep.Length != 2)
                    {
                        return(false);
                    }

                    if (!int.TryParse(sep[0], NumberStyles.None, CultureInfo.InvariantCulture, out nvalue))
                    {
                        return(false);
                    }

                    value.From = nvalue;

                    if (!int.TryParse(sep[1], NumberStyles.None, CultureInfo.InvariantCulture, out nvalue))
                    {
                        return(false);
                    }

                    value.To = nvalue;
                }
                else
                {
                    value.From = nvalue;

                    t = lexer.Scan();
                    if (t != Token.Type.SeparatorDash)
                    {
                        return(false);
                    }

                    t = lexer.Scan();

                    if (!lexer.TryGetNumericValue(t, out nvalue))
                    {
                        return(false);
                    }

                    value.To = nvalue;
                }
            }

            t = lexer.Scan();

            if (t != Token.Type.SeparatorSlash)
            {
                return(false);
            }

            t = lexer.Scan();

            if (!lexer.IsStarStringValue(t))
            {
                if (!lexer.TryGetNumericValue(t, out nvalue))
                {
                    return(false);
                }

                value.Length = nvalue;
            }

            t = lexer.Scan();

            if (t != Token.Type.End)
            {
                return(false);
            }

            parsedValue = value;

            return(true);
        }
		static bool TryParseElement (Lexer lexer, out TransferCodingWithQualityHeaderValue parsedValue, out Token t)
		{
			parsedValue = null;

			t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			var result = new TransferCodingWithQualityHeaderValue ();
			result.value = lexer.GetStringValue (t);

			t = lexer.Scan ();

			// Parameters parsing
			if (t == Token.Type.SeparatorSemicolon && (!NameValueHeaderValue.TryParseParameters (lexer, out result.parameters, out t) || t != Token.Type.End))
				return false;

			parsedValue = result;
			return true;
		}
		static bool TryParseElement (Lexer lexer, out StringWithQualityHeaderValue parsedValue, out Token t)
		{
			parsedValue = null;
			t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			var value = new StringWithQualityHeaderValue ();
			value.Value = lexer.GetStringValue (t);

			t = lexer.Scan ();
			if (t == Token.Type.SeparatorSemicolon) {
				t = lexer.Scan ();
				if (t != Token.Type.Token)
					return false;

				var s = lexer.GetStringValue (t);
				if (s != "q" && s != "Q")
					return false;

				t = lexer.Scan ();
				if (t != Token.Type.SeparatorEqual)
					return false;

				t = lexer.Scan ();

				double d;
				if (!lexer.TryGetDoubleValue (t, out d))
					return false;

				if (d > 1)
					return false;

				value.Quality = d;

				t = lexer.Scan ();
			}

			parsedValue = value;
			return true;
		}
Exemple #23
0
		static bool TryParseElement (Lexer lexer, out EntityTagHeaderValue parsedValue, out Token t)
		{
			parsedValue = null;

			t = lexer.Scan ();
			bool is_weak = false;

			if (t == Token.Type.Token) {
				var s = lexer.GetStringValue (t);
				if (s == "*") {
					parsedValue = any;

					t = lexer.Scan ();
					return true;
				}

				if (s != "W" || lexer.PeekChar () != '/')
					return false;

				is_weak = true;
				lexer.EatChar ();
				t = lexer.Scan ();
			}

			if (t != Token.Type.QuotedString)
				return false;

			parsedValue = new EntityTagHeaderValue ();
			parsedValue.Tag = lexer.GetStringValue (t);
			parsedValue.IsWeak = is_weak;

			t = lexer.Scan ();

			return true;
		}
Exemple #24
0
		static bool TryParseElement (Lexer lexer, out NameValueHeaderValue parsedValue, out Token t)
		{
			parsedValue = null;

			t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			parsedValue = new NameValueHeaderValue () {
				Name = lexer.GetStringValue (t),
			};

			t = lexer.Scan ();
			if (t == Token.Type.SeparatorEqual) {
				t = lexer.Scan ();

				if (t == Token.Type.Token || t == Token.Type.QuotedString) {
					parsedValue.value = lexer.GetStringValue (t);
					t = lexer.Scan ();
				} else {
					return false;
				}
			}

			return true;
		}
		static bool TryParseElement (Lexer lexer, out NameValueWithParametersHeaderValue parsedValue, out Token t)
		{
			parsedValue = null;

			t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			parsedValue = new NameValueWithParametersHeaderValue () {
				Name = lexer.GetStringValue (t),
			};

			t = lexer.Scan ();
			if (t == Token.Type.SeparatorEqual) {
				t = lexer.Scan ();

				if (t == Token.Type.Token || t == Token.Type.QuotedString) {
					parsedValue.value = lexer.GetStringValue (t);
					t = lexer.Scan ();
				} else {
					return false;
				}
			}

			if (t == Token.Type.SeparatorSemicolon) {
				List<NameValueHeaderValue> result;
				if (!TryParseParameters (lexer,  out result, out t))
					return false;

				parsedValue.parameters = result;
			}

			return true;
		}
Exemple #26
0
		static bool TryParseElement (Lexer lexer, out ViaHeaderValue parsedValue, out Token t)	
		{
			parsedValue = null;

			t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			var next = lexer.Scan ();
			ViaHeaderValue value = new ViaHeaderValue ();

			if (next == Token.Type.SeparatorSlash) {
				next = lexer.Scan ();
				if (next != Token.Type.Token)
					return false;

				value.ProtocolName = lexer.GetStringValue (t);
				value.ProtocolVersion = lexer.GetStringValue (next);

				next = lexer.Scan ();
			} else {
				value.ProtocolVersion = lexer.GetStringValue (t);
			}

			if (next != Token.Type.Token)
				return false;

			if (lexer.PeekChar () == ':') {
				lexer.EatChar ();

				t = lexer.Scan ();
				if (t != Token.Type.Token)
					return false;
			} else {
				t = next;
			}

			value.ReceivedBy = lexer.GetStringValue (next, t);

			string comment;
			if (lexer.ScanCommentOptional (out comment, out t)) {
				t = lexer.Scan ();
			}

			value.Comment = comment;
			parsedValue = value;
			return true;
		}
Exemple #27
0
        public static bool TryParse(string input, out CacheControlHeaderValue parsedValue)
        {
            parsedValue = null;
            if (input == null)
            {
                return(true);
            }

            var value = new CacheControlHeaderValue();

            var   lexer = new Lexer(input);
            Token t;

            do
            {
                t = lexer.Scan();
                if (t != Token.Type.Token)
                {
                    return(false);
                }

                string   s          = lexer.GetStringValue(t);
                bool     token_read = false;
                TimeSpan?ts;
                switch (s)
                {
                case "no-store":
                    value.NoStore = true;
                    break;

                case "no-transform":
                    value.NoTransform = true;
                    break;

                case "only-if-cached":
                    value.OnlyIfCached = true;
                    break;

                case "public":
                    value.Public = true;
                    break;

                case "must-revalidate":
                    value.MustRevalidate = true;
                    break;

                case "proxy-revalidate":
                    value.ProxyRevalidate = true;
                    break;

                case "max-stale":
                    value.MaxStale = true;
                    t = lexer.Scan();
                    if (t != Token.Type.SeparatorEqual)
                    {
                        token_read = true;
                        break;
                    }

                    t = lexer.Scan();
                    if (t != Token.Type.Token)
                    {
                        return(false);
                    }

                    ts = lexer.TryGetTimeSpanValue(t);
                    if (ts == null)
                    {
                        return(false);
                    }

                    value.MaxStaleLimit = ts;
                    break;

                case "max-age":
                case "s-maxage":
                case "min-fresh":
                    t = lexer.Scan();
                    if (t != Token.Type.SeparatorEqual)
                    {
                        return(false);
                    }

                    t = lexer.Scan();
                    if (t != Token.Type.Token)
                    {
                        return(false);
                    }

                    ts = lexer.TryGetTimeSpanValue(t);
                    if (ts == null)
                    {
                        return(false);
                    }

                    switch (s.Length)
                    {
                    case 7:
                        value.MaxAge = ts;
                        break;

                    case 8:
                        value.SharedMaxAge = ts;
                        break;

                    default:
                        value.MinFresh = ts;
                        break;
                    }

                    break;

                case "private":
                case "no-cache":
                    if (s.Length == 7)
                    {
                        value.Private = true;
                    }
                    else
                    {
                        value.NoCache = true;
                    }

                    t = lexer.Scan();
                    if (t != Token.Type.SeparatorEqual)
                    {
                        token_read = true;
                        break;
                    }

                    t = lexer.Scan();
                    if (t != Token.Type.QuotedString)
                    {
                        return(false);
                    }

                    foreach (var entry in lexer.GetQuotedStringValue(t).Split(','))
                    {
                        var qs = entry.Trim('\t', ' ');

                        if (s.Length == 7)
                        {
                            value.PrivateHeaders.Add(qs);
                        }
                        else
                        {
                            value.NoCache = true;
                            value.NoCacheHeaders.Add(qs);
                        }
                    }
                    break;

                default:
                    string name   = lexer.GetStringValue(t);
                    string svalue = null;

                    t = lexer.Scan();
                    if (t == Token.Type.SeparatorEqual)
                    {
                        t = lexer.Scan();
                        switch (t.Kind)
                        {
                        case Token.Type.Token:
                        case Token.Type.QuotedString:
                            svalue = lexer.GetStringValue(t);
                            break;

                        default:
                            return(false);
                        }
                    }
                    else
                    {
                        token_read = true;
                    }

                    value.Extensions.Add(NameValueHeaderValue.Create(name, svalue));
                    break;
                }

                if (!token_read)
                {
                    t = lexer.Scan();
                }
            } while (t == Token.Type.SeparatorComma);

            if (t != Token.Type.End)
            {
                return(false);
            }

            parsedValue = value;
            return(true);
        }
Exemple #28
0
        static bool TryParseElement(Lexer lexer, out ViaHeaderValue parsedValue, out Token t)
        {
            parsedValue = null;

            t = lexer.Scan();
            if (t != Token.Type.Token)
            {
                return(false);
            }

            var            next  = lexer.Scan();
            ViaHeaderValue value = new ViaHeaderValue();

            if (next == Token.Type.SeparatorSlash)
            {
                next = lexer.Scan();
                if (next != Token.Type.Token)
                {
                    return(false);
                }

                value.ProtocolName    = lexer.GetStringValue(t);
                value.ProtocolVersion = lexer.GetStringValue(next);

                next = lexer.Scan();
            }
            else
            {
                value.ProtocolVersion = lexer.GetStringValue(t);
            }

            if (next != Token.Type.Token)
            {
                return(false);
            }

            if (lexer.PeekChar() == ':')
            {
                lexer.EatChar();

                t = lexer.Scan();
                if (t != Token.Type.Token)
                {
                    return(false);
                }
            }
            else
            {
                t = next;
            }

            value.ReceivedBy = lexer.GetStringValue(next, t);

            string comment;

            if (lexer.ScanCommentOptional(out comment, out t))
            {
                t = lexer.Scan();
            }

            value.Comment = comment;
            parsedValue   = value;
            return(true);
        }
Exemple #29
0
        public static bool TryParse(string input, out RangeHeaderValue parsedValue)
        {
            parsedValue = null;

            var lexer = new Lexer(input);
            var t     = lexer.Scan();

            if (t != Token.Type.Token)
            {
                return(false);
            }

            var value = new RangeHeaderValue();

            value.unit = lexer.GetStringValue(t);

            t = lexer.Scan();
            if (t != Token.Type.SeparatorEqual)
            {
                return(false);
            }

            bool token_read;

            do
            {
                int?from = null, to = null;
                int number;
                token_read = false;

                t = lexer.Scan();
                switch (t.Kind)
                {
                case Token.Type.SeparatorDash:
                    t = lexer.Scan();
                    if (!lexer.TryGetNumericValue(t, out number))
                    {
                        return(false);
                    }

                    to = number;
                    break;

                case Token.Type.Token:
                    string s      = lexer.GetStringValue(t);
                    var    values = s.Split(new [] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                    if (!int.TryParse(values[0], out number))
                    {
                        return(false);
                    }

                    switch (values.Length)
                    {
                    case 1:
                        t    = lexer.Scan();
                        from = number;
                        switch (t.Kind)
                        {
                        case Token.Type.SeparatorDash:
                            t = lexer.Scan();
                            if (t != Token.Type.Token)
                            {
                                token_read = true;
                                break;
                            }

                            if (!lexer.TryGetNumericValue(t, out number))
                            {
                                return(false);
                            }

                            to = number;
                            if (to < from)
                            {
                                return(false);
                            }

                            break;

                        case Token.Type.End:
                            if (s.Length > 0 && s [s.Length - 1] != '-')
                            {
                                return(false);
                            }

                            token_read = true;
                            break;

                        case Token.Type.SeparatorComma:
                            token_read = true;
                            break;

                        default:
                            return(false);
                        }
                        break;

                    case 2:
                        from = number;

                        if (!int.TryParse(values[1], out number))
                        {
                            return(false);
                        }

                        to = number;
                        if (to < from)
                        {
                            return(false);
                        }

                        break;

                    default:
                        return(false);
                    }

                    break;

                default:
                    return(false);
                }

                value.Ranges.Add(new RangeItemHeaderValue(from, to));
                if (!token_read)
                {
                    t = lexer.Scan();
                }
            } while (t == Token.Type.SeparatorComma);

            if (t != Token.Type.End)
            {
                return(false);
            }

            parsedValue = value;
            return(true);
        }
Exemple #30
0
		public static bool TryParse (string input, out WarningHeaderValue parsedValue)
		{
			parsedValue = null;

			var lexer = new Lexer (input);
			var t = lexer.Scan ();

			if (t != Token.Type.Token)
				return false;

			int code;
			if (!lexer.TryGetNumericValue (t, out code) || !IsCodeValid (code))
				return false;

			t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			var next = t;
			if (lexer.PeekChar () == ':') {
				lexer.EatChar ();

				next = lexer.Scan ();
				if (next != Token.Type.Token)
					return false;
			}

			var value = new WarningHeaderValue ();
			value.Code = code;
			value.Agent = lexer.GetStringValue (t, next);

			t = lexer.Scan ();
			if (t != Token.Type.QuotedString)
				return false;

			value.Text = lexer.GetStringValue (t);

			t = lexer.Scan ();
			if (t == Token.Type.QuotedString) {
				DateTimeOffset date;
				if (!lexer.TryGetDateValue (t, out date))
					return false;

				value.Date = date;
				t = lexer.Scan ();
			}

			if (t != Token.Type.End)
				return false;

			parsedValue = value;
			return true;
		}
		public static bool TryParse (string input, out RangeConditionHeaderValue parsedValue)
		{
			parsedValue = null;

			var lexer = new Lexer (input);
			var t = lexer.Scan ();
			bool is_weak;

			if (t == Token.Type.Token) {
				if (lexer.GetStringValue (t) != "W") {
					DateTimeOffset date;
					if (!Lexer.TryGetDateValue (input, out date))
						return false;

					parsedValue = new RangeConditionHeaderValue (date);
					return true;
				}

				if (lexer.PeekChar () != '/')
					return false;

				is_weak = true;
				lexer.EatChar ();
				t = lexer.Scan ();
			} else {
				is_weak = false;
			}

			if (t != Token.Type.QuotedString)
				return false;

			if (lexer.Scan () != Token.Type.End)
				return false;

			parsedValue = new RangeConditionHeaderValue (
				new EntityTagHeaderValue () {
					Tag = lexer.GetStringValue (t),
					IsWeak = is_weak
				});

			return true;
		}
Exemple #32
0
		internal static bool TryParseParameters (Lexer lexer, out List<NameValueHeaderValue> result, out Token t)
		{		
			var list = new List<NameValueHeaderValue> ();
			result = null;

			while (true) {
				var attr = lexer.Scan ();
				if (attr != Token.Type.Token) {
					t = Token.Empty;
					return false;
				}

				string value = null;

				t = lexer.Scan ();
				if (t == Token.Type.SeparatorEqual) {
					t = lexer.Scan ();
					if (t != Token.Type.Token && t != Token.Type.QuotedString)
						return false;

					value = lexer.GetStringValue (t);

					t = lexer.Scan ();
				}

				list.Add (new NameValueHeaderValue () {
					Name = lexer.GetStringValue (attr),
					value = value
				});

				if (t == Token.Type.SeparatorSemicolon)
					continue;

				result = list;
				return true;
			}
		}
Exemple #33
0
		internal static Token? TryParseMediaType (Lexer lexer, out string media)
		{
			media = null;

			var token = lexer.Scan ();
			if (token != Token.Type.Token)
				return null;

			if (lexer.Scan () != Token.Type.SeparatorSlash)
				return null;

			var token2 = lexer.Scan ();
			if (token2 != Token.Type.Token)
				return null;

			media = lexer.GetStringValue (token) + "/" + lexer.GetStringValue (token2);

			return lexer.Scan ();
		}
Exemple #34
0
		static bool TryParseElement (Lexer lexer, out ProductHeaderValue parsedValue, out Token t)
		{
			parsedValue = null;

			t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			parsedValue = new ProductHeaderValue ();
			parsedValue.Name = lexer.GetStringValue (t);

			t = lexer.Scan ();
			if (t == Token.Type.SeparatorSlash) {
				t = lexer.Scan ();
				if (t != Token.Type.Token)
					return false;

				parsedValue.Version = lexer.GetStringValue (t);
				t = lexer.Scan ();
			}

			return true;
		}
Exemple #35
0
		static bool TryParseCollection (Lexer lexer, out List<NameValueHeaderValue> result, Token.Type separator)
		{		
			var list = new List<NameValueHeaderValue> ();
			result = null;

			Token t;

			do {
				var attr = lexer.Scan ();
				if (attr != Token.Type.Token)
					return false;

				string value = null;

				t = lexer.Scan ();
				if (t == Token.Type.SeparatorEqual) {
					t = lexer.Scan ();
					if (t != Token.Type.Token && t != Token.Type.QuotedString)
						return false;

					value = lexer.GetStringValue (t);

					t = lexer.Scan ();
				}

				if (t == separator|| t == Token.Type.End) {
					list.Add (new NameValueHeaderValue () {
						Name = lexer.GetStringValue (attr),
						value = value
					});
				} else {
					return false;
				}

			} while (t == separator);

			result = list;
			return true;
		}
		public static bool TryParse (string input, out ContentDispositionHeaderValue parsedValue)
		{
			parsedValue = null;

			var lexer = new Lexer (input);
			var t = lexer.Scan ();
			if (t.Kind != Token.Type.Token)
				return false;

			List<NameValueHeaderValue> parameters = null;
			var type = lexer.GetStringValue (t);

			t = lexer.Scan ();

			switch (t.Kind) {
			case Token.Type.SeparatorSemicolon:
				if (!NameValueHeaderValue.TryParseParameters (lexer, out parameters, out t) || t != Token.Type.End)
					return false;
				break;
			case Token.Type.End:
				break;
			default:
				return false;
			}

			parsedValue = new ContentDispositionHeaderValue () {
				dispositionType = type,
				parameters = parameters
			};

			return true;
		}
Exemple #37
0
		public static bool TryParse (string input, out NameValueHeaderValue parsedValue)
		{
			parsedValue = null;

			var lexer = new Lexer (input);
			var t = lexer.Scan ();
			if (t != Token.Type.Token && t != Token.Type.QuotedString)
				return false;

			string v = null;
			var token2 = lexer.Scan ();
			if (token2 != Token.Type.End) {
				if (token2 != Token.Type.SeparatorEqual)
					return false;

				token2 = lexer.Scan ();

				if (token2 == Token.Type.Token || token2 == Token.Type.QuotedString) {
					v = lexer.GetStringValue (token2);
					token2 = lexer.Scan ();
				}

				if (token2 != Token.Type.End)
					return false;
			}

			parsedValue = new NameValueHeaderValue () {
				Name = lexer.GetStringValue (t),
				value = v
			};

			return true;
		}
		public static bool TryParse (string input, out ContentRangeHeaderValue parsedValue)
		{
			parsedValue = null;

			var lexer = new Lexer (input);
			var t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			var value = new ContentRangeHeaderValue ();
			value.unit = lexer.GetStringValue (t);

			t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			int nvalue;
			if (!lexer.IsStarStringValue (t)) {
				if (!lexer.TryGetNumericValue (t, out nvalue)) {
					var s = lexer.GetStringValue (t);
					if (s.Length < 3)
						return false;

					var sep = s.Split ('-');
					if (sep.Length != 2)
						return false;

					if (!int.TryParse (sep[0], NumberStyles.None, CultureInfo.InvariantCulture, out nvalue))
						return false;

					value.From = nvalue;

					if (!int.TryParse (sep[1], NumberStyles.None, CultureInfo.InvariantCulture, out nvalue))
						return false;

					value.To = nvalue;
				} else {
					value.From = nvalue;

					t = lexer.Scan (recognizeDash: true);
					if (t != Token.Type.SeparatorDash)
						return false;

					t = lexer.Scan ();

					if (!lexer.TryGetNumericValue (t, out nvalue))
						return false;

					value.To = nvalue;
				}
			}

			t = lexer.Scan ();

			if (t != Token.Type.SeparatorSlash)
				return false;

			t = lexer.Scan ();

			if (!lexer.IsStarStringValue (t)) {
				long lvalue;
				if (!lexer.TryGetNumericValue (t, out lvalue))
					return false;

				value.Length = lvalue;
			}

			t = lexer.Scan ();

			if (t != Token.Type.End)
				return false;

			parsedValue = value;
 
			return true;
		}
        public static bool TryParse(string input, out CacheControlHeaderValue parsedValue)
        {
            parsedValue = null;
            if (input == null)
                return true;

            var value = new CacheControlHeaderValue ();

            var lexer = new Lexer (input);
            Token t;
            do {
                t = lexer.Scan ();
                if (t != Token.Type.Token)
                    return false;

                string s = lexer.GetStringValue (t);
                bool token_read = false;
                TimeSpan? ts;
                switch (s) {
                case "no-store":
                    value.NoStore = true;
                    break;
                case "no-transform":
                    value.NoTransform = true;
                    break;
                case "only-if-cached":
                    value.OnlyIfCached = true;
                    break;
                case "public":
                    value.Public = true;
                    break;
                case "must-revalidate":
                    value.MustRevalidate = true;
                    break;
                case "proxy-revalidate":
                    value.ProxyRevalidate = true;
                    break;
                case "max-stale":
                    value.MaxStale = true;
                    t = lexer.Scan ();
                    if (t != Token.Type.SeparatorEqual) {
                        token_read = true;
                        break;
                    }

                    t = lexer.Scan ();
                    if (t != Token.Type.Token)
                        return false;

                    ts = lexer.TryGetTimeSpanValue (t);
                    if (ts == null)
                        return false;

                    value.MaxStaleLimit = ts;
                    break;
                case "max-age":
                case "s-maxage":
                case "min-fresh":
                    t = lexer.Scan ();
                    if (t != Token.Type.SeparatorEqual) {
                        return false;
                    }

                    t = lexer.Scan ();
                    if (t != Token.Type.Token)
                        return false;

                    ts = lexer.TryGetTimeSpanValue (t);
                    if (ts == null)
                        return false;

                    switch (s.Length) {
                    case 7:
                        value.MaxAge = ts;
                        break;
                    case 8:
                        value.SharedMaxAge = ts;
                        break;
                    default:
                        value.MinFresh = ts;
                        break;
                    }

                    break;
                case "private":
                case "no-cache":
                    if (s.Length == 7) {
                        value.Private = true;
                    } else {
                        value.NoCache = true;
                    }

                    t = lexer.Scan ();
                    if (t != Token.Type.SeparatorEqual) {
                        token_read = true;
                        break;
                    }

                    t = lexer.Scan ();
                    if (t != Token.Type.QuotedString)
                        return false;

                    foreach (var entry in lexer.GetQuotedStringValue (t).Split (',')) {
                        var qs = entry.Trim ('\t', ' ');

                        if (s.Length == 7) {
                            value.PrivateHeaders.Add (qs);
                        } else {
                            value.NoCache = true;
                            value.NoCacheHeaders.Add (qs);
                        }
                    }
                    break;
                default:
                    string name = lexer.GetStringValue (t);
                    string svalue = null;

                    t = lexer.Scan ();
                    if (t == Token.Type.SeparatorEqual) {
                        t = lexer.Scan ();
                        switch (t.Kind) {
                        case Token.Type.Token:
                        case Token.Type.QuotedString:
                            svalue = lexer.GetStringValue (t);
                            break;
                        default:
                            return false;
                        }
                    } else {
                        token_read = true;
                    }

                    value.Extensions.Add (NameValueHeaderValue.Create (name, svalue));
                    break;
                }

                if (!token_read)
                    t = lexer.Scan ();
            } while (t == Token.Type.SeparatorComma);

            if (t != Token.Type.End)
                return false;

            parsedValue = value;
            return true;
        }
Exemple #40
0
		public static bool TryParse (string input, out RangeHeaderValue parsedValue)
		{
			parsedValue = null;

			var lexer = new Lexer (input);
			var t = lexer.Scan ();
			if (t != Token.Type.Token)
				return false;

			var value = new RangeHeaderValue ();
			value.unit = lexer.GetStringValue (t);

			t = lexer.Scan ();
			if (t != Token.Type.SeparatorEqual)
				return false;

			bool token_read;
			do {
				int? from = null, to = null;
				int number;
				token_read = false;

				t = lexer.Scan ();
				switch (t.Kind) {
				case Token.Type.SeparatorDash:
					t = lexer.Scan ();
					if (!lexer.TryGetNumericValue (t, out number))
						return false;

					to = number;
					break;
				case Token.Type.Token:
					string s = lexer.GetStringValue (t);
					var values = s.Split (new [] { '-' }, StringSplitOptions.RemoveEmptyEntries);
					if (!int.TryParse (values[0], out number))
						return false;

					switch (values.Length) {
					case 1:
						t = lexer.Scan ();
						switch (t.Kind) {
						case Token.Type.SeparatorDash:
							from = number;

							t = lexer.Scan ();
							if (t != Token.Type.Token) {
								token_read = true;
								break;
							}

							if (!lexer.TryGetNumericValue (t, out number))
								return false;

							to = number;
							if (to < from)
								return false;

							break;
						default:
							return false;
						}
						break;
					case 2:
						from = number;

						if (!int.TryParse (values[1], out number))
							return false;

						to = number;
						if (to < from)
							return false;

						break;
					default:
						return false;
					}

					break;
				default:
					return false;
				}

				value.Ranges.Add (new RangeItemHeaderValue (from, to));
				if (!token_read)
					t = lexer.Scan ();

			} while (t == Token.Type.SeparatorComma);

			if (t != Token.Type.End)
				return false;

			parsedValue = value;
			return true;
		}
		static bool TryParseElement (Lexer lexer, out AuthenticationHeaderValue parsedValue, out Token t)
		{
			t = lexer.Scan ();
			if (t != Token.Type.Token) {
				parsedValue = null;
				return false;
			}

			parsedValue = new AuthenticationHeaderValue ();
			parsedValue.Scheme = lexer.GetStringValue (t);

			t = lexer.Scan ();
			if (t == Token.Type.Token) {
				// TODO: Wrong with multi value parsing
				parsedValue.Parameter = lexer.GetRemainingStringValue (t.StartPosition);
				t = new Token (Token.Type.End, 0, 0);
			}

			return true;
		}