Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / Lexer.cs
1 //
2 // Lexer.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Globalization;
30
31 namespace System.Net.Http.Headers
32 {
33         struct Token
34         {
35                 public enum Type
36                 {
37                         Error,
38                         End,
39                         Token,
40                         QuotedString,
41                         SeparatorEqual,
42                         SeparatorSemicolon,
43                         SeparatorSlash,
44                         SeparatorDash,
45                         SeparatorComma,
46                         OpenParens,
47                 }
48
49                 public static readonly Token Empty = new Token (Type.Token, 0, 0);
50
51                 readonly Type type;
52
53                 public Token (Type type, int startPosition, int endPosition)
54                         : this ()
55                 {
56                         this.type = type;
57                         StartPosition = startPosition;
58                         EndPosition = endPosition;
59                 }
60
61                 public int StartPosition { get; private set; }
62                 public int EndPosition { get; private set; }
63
64                 public Type Kind {
65                         get {
66                                 return type;
67                         }
68                 }
69
70                 public static implicit operator Token.Type (Token token)
71                 {
72                         return token.type;
73                 }
74
75                 public override string ToString ()
76                 {
77                         return type.ToString ();
78                 }
79         }
80
81         class Lexer
82         {
83                 // any CHAR except CTLs or separators
84                 static readonly bool[] token_chars = {
85                         /*0*/   false, false, false, false, false, false, false, false, false, false,
86                         /*10*/  false, false, false, false, false, false, false, false, false, false,
87                         /*20*/  false, false, false, false, false, false, false, false, false, false,
88                         /*30*/  false, false, false, true, false, true, true, true, true, true,
89                         /*40*/  false, false, true, true, false, true, true, false, true, true,
90                         /*50*/  true, true, true, true, true, true, true, true, false, false,
91                         /*60*/  false, false, false, false, false, true, true, true, true, true,
92                         /*70*/  true, true, true, true, true, true, true, true, true, true,
93                         /*80*/  true, true, true, true, true, true, true, true, true, true,
94                         /*90*/  true, false, false, false, true, true, true, true, true, true,
95                         /*100*/ true, true, true, true, true, true, true, true, true, true,
96                         /*110*/ true, true, true, true, true, true, true, true, true, true,
97                         /*120*/ true, true, true, false, true, false
98                         };
99
100                 static readonly int last_token_char = token_chars.Length;
101                 static readonly string[] dt_formats = new[] {
102                                 "r",
103                                 "dddd, dd'-'MMM'-'yy HH:mm:ss 'GMT'",
104                                 "ddd MMM d HH:mm:ss yyyy",
105                                 "d MMM yy H:m:s",
106                                 "ddd, d MMM yyyy H:m:s zzz"
107                 };
108
109                 readonly string s;
110                 int pos;
111
112                 public Lexer (string stream)
113                 {
114                         this.s = stream;
115                 }
116
117                 public string GetStringValue (Token token)
118                 {
119                         return s.Substring (token.StartPosition, token.EndPosition - token.StartPosition);
120                 }
121
122                 public string GetStringValue (Token start, Token end)
123                 {
124                         return s.Substring (start.StartPosition, end.EndPosition - start.StartPosition);
125                 }
126
127                 public string GetQuotedStringValue (Token start)
128                 {
129                         return s.Substring (start.StartPosition + 1, start.EndPosition - start.StartPosition - 2);
130                 }
131
132                 public string GetRemainingStringValue (int position)
133                 {
134                         return position > s.Length ? null : s.Substring (position);
135                 }
136
137                 public bool IsStarStringValue (Token token)
138                 {
139                         return (token.EndPosition - token.StartPosition) == 1 && s[token.StartPosition] == '*';
140                 }
141
142                 public bool TryGetNumericValue (Token token, out int value)
143                 {
144                         return int.TryParse (GetStringValue (token), NumberStyles.None, CultureInfo.InvariantCulture, out value);
145                 }
146
147                 public TimeSpan? TryGetTimeSpanValue (Token token)
148                 {
149                         int seconds;
150                         if (TryGetNumericValue (token, out seconds)) {
151                                 return TimeSpan.FromSeconds (seconds);
152                         }
153
154                         return null;
155                 }
156
157                 public bool TryGetDateValue (Token token, out DateTimeOffset value)
158                 {
159                         string text = token == Token.Type.QuotedString ?
160                                 s.Substring (token.StartPosition + 1, token.EndPosition - token.StartPosition - 2) :
161                                 GetStringValue (token);
162
163                         return TryGetDateValue (text, out value);
164                 }
165
166                 public static bool TryGetDateValue (string text, out DateTimeOffset value)
167                 {
168                         const DateTimeStyles DefaultStyles = DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces;
169
170                         return DateTimeOffset.TryParseExact (text, dt_formats, DateTimeFormatInfo.InvariantInfo, DefaultStyles, out value);
171                 }
172
173                 public bool TryGetDoubleValue (Token token, out double value)
174                 {
175                         string s = GetStringValue (token);
176                         return double.TryParse (s, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out value);
177                 }
178
179                 public static bool IsValidToken (string input)
180                 {
181                         int i = 0;
182                         //
183                         // any CHAR except CTLs or separator
184                         //
185                         for (; i < input.Length; ++i) {
186                                 char s = input[i];
187                                 if (s > last_token_char || !token_chars[s])
188                                         return false;
189                         }
190
191                         return i > 0;
192                 }
193
194                 public void EatChar ()
195                 {
196                         ++pos;
197                 }
198
199                 public int PeekChar ()
200                 {
201                         return pos < s.Length ? s[pos] : -1;
202                 }
203
204                 public bool ScanCommentOptional (out string value)
205                 {
206                         Token t;
207                         if (ScanCommentOptional (out value, out t))
208                                 return true;
209
210                         return t == Token.Type.End;
211                 }
212
213                 public bool ScanCommentOptional (out string value, out Token readToken)
214                 {
215                         readToken = Scan ();
216                         if (readToken != Token.Type.OpenParens) {
217                                 value = null;
218                                 return false;
219                         }
220
221                         while (pos < s.Length) {
222                                 var ch = s[pos];
223                                 if (ch == ')') {
224                                         ++pos;
225                                         var start = readToken.StartPosition;
226                                         value = s.Substring (start, pos - start);
227                                         return true;
228                                 }
229
230                                 // any OCTET except CTLs, but including LWS
231                                 if (ch < 32 || ch > 126)
232                                         break;
233
234                                 ++pos;
235                         }
236
237                         value = null;
238                         return false;
239                 }
240
241                 public Token Scan ()
242                 {
243                         int start = pos;
244                         if (s == null)
245                                 return new Token (Token.Type.Error, 0, 0);
246
247                         Token.Type ttype;
248                         if (pos >= s.Length) {
249                                 ttype = Token.Type.End;
250                         } else {
251                                 ttype = Token.Type.Error;
252                         start:
253                                 char ch = s[pos++];
254                                 switch (ch) {
255                                 case ' ':
256                                 case '\t':
257                                         if (pos == s.Length) {
258                                                 ttype = Token.Type.End;
259                                                 break;
260                                         }
261
262                                         goto start;
263                                 case '=':
264                                         ttype = Token.Type.SeparatorEqual;
265                                         break;
266                                 case ';':
267                                         ttype = Token.Type.SeparatorSemicolon;
268                                         break;
269                                 case '/':
270                                         ttype = Token.Type.SeparatorSlash;
271                                         break;
272                                 case '-':
273                                         ttype = Token.Type.SeparatorDash;
274                                         break;
275                                 case ',':
276                                         ttype = Token.Type.SeparatorComma;
277                                         break;
278                                 case '"':
279                                         // Quoted string
280                                         start = pos - 1;
281                                         while (pos < s.Length) {
282                                                 ch = s[pos];
283                                                 if (ch == '"') {
284                                                         ++pos;
285                                                         ttype = Token.Type.QuotedString;
286                                                         break;
287                                                 }
288
289                                                 // any OCTET except CTLs, but including LWS
290                                                 if (ch < 32 || ch > 126)
291                                                         break;
292
293                                                 ++pos;
294                                         }
295
296                                         break;
297                                 case '(':
298                                         start = pos - 1;
299                                         ttype = Token.Type.OpenParens;
300                                         break;
301                                 default:
302                                         if (ch <= last_token_char && token_chars[ch]) {
303                                                 start = pos - 1;
304
305                                                 ttype = Token.Type.Token;
306                                                 while (pos < s.Length) {
307                                                         ch = s[pos];
308                                                         if (ch > last_token_char || !token_chars[ch]) {
309                                                                 break;
310                                                         }
311
312                                                         ++pos;
313                                                 }
314                                         }
315
316                                         break;
317                                 }
318                         }
319
320                         return new Token (ttype, start, pos);
321                 }
322         }
323 }