Exemplo n.º 1
0
		public static bool TryParse (string input, out RetryConditionHeaderValue parsedValue)
		{
			parsedValue = null;

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

			var ts = lexer.TryGetTimeSpanValue (t);
			if (ts != null) {
				if (lexer.Scan () != Token.Type.End)
					return false;

				parsedValue = new RetryConditionHeaderValue (ts.Value);
			} else {
				DateTimeOffset date;
				if (!Lexer.TryGetDateValue (input, out date))
					return false;

				parsedValue = new RetryConditionHeaderValue (date);
			}

			return true;
		}
		public static bool TryParse (string input, out MediaTypeWithQualityHeaderValue parsedValue)
		{
			var lexer = new Lexer (input);
			Token token;
			if (TryParseElement (lexer, out parsedValue, out token) && token == Token.Type.End)
				return true;

			parsedValue = null;
			return false;
		}
Exemplo n.º 3
0
Arquivo: Parser.cs Projeto: RAOF/mono
			public static void CheckQuotedString (string s)
			{
				if (s == null)
					throw new ArgumentNullException ();

				var lexer = new Lexer (s);
				if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
					return;

				if (s.Length == 0)
					throw new ArgumentException ();

				throw new FormatException (s);
			}
Exemplo n.º 4
0
Arquivo: Parser.cs Projeto: RAOF/mono
			public static void CheckComment (string s)
			{
				if (s == null)
					throw new ArgumentNullException ();

				var lexer = new Lexer (s);

				string temp;
				if (!lexer.ScanCommentOptional (out temp)) {
					if (s.Length == 0)
						throw new ArgumentException ();

					throw new FormatException (s);
				}
			}
		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 MediaTypeWithQualityHeaderValue parsedValue, out Token t)
		{
			parsedValue = null;

			string media;
			List<NameValueHeaderValue> parameters = null;
			var token = TryParseMediaType (lexer, out media);
			if (token == null) {
				t = Token.Empty;
				return false;
			}

			t = token.Value;
			if (t == Token.Type.SeparatorSemicolon && (!NameValueHeaderValue.TryParseParameters (lexer, out parameters, out t) || t != Token.Type.End))
				return false;

			parsedValue = new MediaTypeWithQualityHeaderValue ();
			parsedValue.media_type = media;
			parsedValue.parameters = parameters;
			return true;
		}
Exemplo n.º 7
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;
		}
Exemplo n.º 8
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;
		}
Exemplo n.º 9
0
		internal static bool TryParse (string input, int minimalCount, out List<ProductInfoHeaderValue> result)
		{
			var list = new List<ProductInfoHeaderValue> ();
			var lexer = new Lexer (input);
			result = null;

			while (true) {
				ProductInfoHeaderValue element;
				if (!TryParseElement (lexer, out element))
					return false;

				if (element == null) {
					if (list != null && minimalCount <= list.Count) {
						result = list;
						return true;
					}

					return false;
				}

				list.Add (element);

				// Separator parsing
				switch (lexer.PeekChar ()) {
				case ' ':
				case '\t':
					lexer.EatChar ();
					continue;
				case -1:
					if (minimalCount <= list.Count) {
						result = list;
						return true;
					}

					break;
				}
					
				return false;
			}
		}
Exemplo n.º 10
0
		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;
		}
Exemplo n.º 11
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;
		}
Exemplo n.º 12
0
		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;
		}
Exemplo n.º 13
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);
        }
		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;
		}
Exemplo n.º 15
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;
		}
Exemplo n.º 16
0
		public static bool TryParse (string input, out ProductInfoHeaderValue parsedValue)
		{
			parsedValue = null;

			var lexer = new Lexer (input);
			if (!TryParseElement (lexer, out parsedValue) || parsedValue == null)
				return false;

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

			return true;
		}
Exemplo n.º 17
0
		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;
		}
Exemplo n.º 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);
        }
Exemplo n.º 19
0
		internal static bool TryParse (string input, int minimalCount, out List<ProductInfoHeaderValue> result)
		{
			var list = new List<ProductInfoHeaderValue> ();
			var lexer = new Lexer (input);
			result = null;

			while (true) {
				ProductInfoHeaderValue element;
				if (!TryParseElement (lexer, out element))
					return false;

				if (element == null) {
					if (list != null && minimalCount <= list.Count) {
						result = list;
						return true;
					}

					return false;
				}

				list.Add (element);
			}
		}
Exemplo n.º 20
0
 public static bool TryParse(string input, out DateTimeOffset result)
 {
     return(Lexer.TryGetDateValue(input, out result));
 }
Exemplo n.º 21
0
		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;
		}
Exemplo n.º 22
0
        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);
            }

            long 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 (!long.TryParse(sep[0], NumberStyles.None, CultureInfo.InvariantCulture, out nvalue))
                    {
                        return(false);
                    }

                    value.From = nvalue;

                    if (!long.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);
        }
Exemplo n.º 23
0
		public static bool TryParse (string input, out MediaTypeHeaderValue parsedValue)
		{
			parsedValue = null;

			var lexer = new Lexer (input);

			string media;
			List<NameValueHeaderValue> parameters = null;
			var token = TryParseMediaType (lexer, out media);
			if (token == null)
				return false;

			switch (token.Value.Kind) {
			case Token.Type.SeparatorSemicolon:
				Token t;
				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 MediaTypeHeaderValue () {
				media_type = media,
				parameters = parameters
			};

			return true;
		}
Exemplo n.º 24
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 ();
		}
Exemplo n.º 25
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;
		}
Exemplo n.º 26
0
		internal static bool TryParseParameters (Lexer lexer, out List<NameValueHeaderValue> result)
		{
			return TryParseCollection (lexer, out result, Token.Type.SeparatorSemicolon);
		}
Exemplo n.º 27
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;
			}
		}
Exemplo n.º 28
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;
		}
Exemplo n.º 29
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;
		}
Exemplo n.º 30
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;
		}
Exemplo n.º 31
0
		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;
        }
Exemplo n.º 33
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;
		}
Exemplo n.º 34
0
		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;
		}