Merge pull request #2720 from mono/fix-39325
[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, true
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 int Position {
118                         get {
119                                 return pos;
120                         }
121                         set {
122                                 pos = value;
123                         }
124                 }
125
126                 public string GetStringValue (Token token)
127                 {
128                         return s.Substring (token.StartPosition, token.EndPosition - token.StartPosition);
129                 }
130
131                 public string GetStringValue (Token start, Token end)
132                 {
133                         return s.Substring (start.StartPosition, end.EndPosition - start.StartPosition);
134                 }
135
136                 public string GetQuotedStringValue (Token start)
137                 {
138                         return s.Substring (start.StartPosition + 1, start.EndPosition - start.StartPosition - 2);
139                 }
140
141                 public string GetRemainingStringValue (int position)
142                 {
143                         return position > s.Length ? null : s.Substring (position);
144                 }
145
146                 public bool IsStarStringValue (Token token)
147                 {
148                         return (token.EndPosition - token.StartPosition) == 1 && s[token.StartPosition] == '*';
149                 }
150
151                 public bool TryGetNumericValue (Token token, out int value)
152                 {
153                         return int.TryParse (GetStringValue (token), NumberStyles.None, CultureInfo.InvariantCulture, out value);
154                 }
155
156                 public bool TryGetNumericValue (Token token, out long value)
157                 {
158                         return long.TryParse (GetStringValue (token), NumberStyles.None, CultureInfo.InvariantCulture, out value);
159                 }
160
161                 public TimeSpan? TryGetTimeSpanValue (Token token)
162                 {
163                         int seconds;
164                         if (TryGetNumericValue (token, out seconds)) {
165                                 return TimeSpan.FromSeconds (seconds);
166                         }
167
168                         return null;
169                 }
170
171                 public bool TryGetDateValue (Token token, out DateTimeOffset value)
172                 {
173                         string text = token == Token.Type.QuotedString ?
174                                 s.Substring (token.StartPosition + 1, token.EndPosition - token.StartPosition - 2) :
175                                 GetStringValue (token);
176
177                         return TryGetDateValue (text, out value);
178                 }
179
180                 public static bool TryGetDateValue (string text, out DateTimeOffset value)
181                 {
182                         const DateTimeStyles DefaultStyles = DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces;
183
184                         return DateTimeOffset.TryParseExact (text, dt_formats, DateTimeFormatInfo.InvariantInfo, DefaultStyles, out value);
185                 }
186
187                 public bool TryGetDoubleValue (Token token, out double value)
188                 {
189                         string s = GetStringValue (token);
190                         return double.TryParse (s, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out value);
191                 }
192
193                 public static bool IsValidToken (string input)
194                 {
195                         int i = 0;
196                         //
197                         // any CHAR except CTLs or separator
198                         //
199                         for (; i < input.Length; ++i) {
200                                 char s = input[i];
201                                 if (!IsValidCharacter (s))
202                                         return false;
203                         }
204
205                         return i > 0;
206                 }
207
208                 public static bool IsValidCharacter (char input)
209                 {
210                         return input < last_token_char && token_chars[input];
211                 }
212
213                 public void EatChar ()
214                 {
215                         ++pos;
216                 }
217
218                 public int PeekChar ()
219                 {
220                         return pos < s.Length ? s[pos] : -1;
221                 }
222
223                 public bool ScanCommentOptional (out string value)
224                 {
225                         Token t;
226                         if (ScanCommentOptional (out value, out t))
227                                 return true;
228
229                         return t == Token.Type.End;
230                 }
231
232                 public bool ScanCommentOptional (out string value, out Token readToken)
233                 {
234                         readToken = Scan ();
235                         if (readToken != Token.Type.OpenParens) {
236                                 value = null;
237                                 return false;
238                         }
239
240                         int parens = 1;
241                         while (pos < s.Length) {
242                                 var ch = s[pos];
243                                 if (ch == '(') {
244                                         ++parens;
245                                         ++pos;
246                                         continue;
247                                 }
248
249                                 if (ch == ')') {
250                                         ++pos;
251                                         if (--parens > 0)
252                                                 continue;
253
254                                         var start = readToken.StartPosition;
255                                         value = s.Substring (start, pos - start);
256                                         return true;
257                                 }
258
259                                 // any OCTET except CTLs, but including LWS
260                                 if (ch < 32 || ch > 126)
261                                         break;
262
263                                 ++pos;
264                         }
265
266                         value = null;
267                         return false;
268                 }
269
270                 public Token Scan (bool recognizeDash = false)
271                 {
272                         int start = pos;
273                         if (s == null)
274                                 return new Token (Token.Type.Error, 0, 0);
275
276                         Token.Type ttype;
277                         if (pos >= s.Length) {
278                                 ttype = Token.Type.End;
279                         } else {
280                                 ttype = Token.Type.Error;
281                         start:
282                                 char ch = s[pos++];
283                                 switch (ch) {
284                                 case ' ':
285                                 case '\t':
286                                         if (pos == s.Length) {
287                                                 ttype = Token.Type.End;
288                                                 break;
289                                         }
290
291                                         goto start;
292                                 case '=':
293                                         ttype = Token.Type.SeparatorEqual;
294                                         break;
295                                 case ';':
296                                         ttype = Token.Type.SeparatorSemicolon;
297                                         break;
298                                 case '/':
299                                         ttype = Token.Type.SeparatorSlash;
300                                         break;
301                                 case '-':
302                                         if (recognizeDash) {
303                                                 ttype = Token.Type.SeparatorDash;
304                                                 break;
305                                         }
306
307                                         goto default;
308                                 case ',':
309                                         ttype = Token.Type.SeparatorComma;
310                                         break;
311                                 case '"':
312                                         // Quoted string
313                                         start = pos - 1;
314                                         while (pos < s.Length) {
315                                                 ch = s [pos++];
316
317                                                 //
318                                                 // The backslash character ("\") MAY be used as a single-character
319                                                 // quoting mechanism only within quoted-string
320                                                 //
321                                                 if (ch == '\\') {
322                                                         if (pos + 1 < s.Length) {
323                                                                 ++pos;
324                                                                 continue;
325                                                         }
326
327                                                         break;
328                                                 }
329
330                                                 if (ch == '"') {
331                                                         ttype = Token.Type.QuotedString;
332                                                         break;
333                                                 }
334                                         }
335
336                                         break;
337                                 case '(':
338                                         start = pos - 1;
339                                         ttype = Token.Type.OpenParens;
340                                         break;
341                                 default:
342                                         if (ch < last_token_char && token_chars[ch]) {
343                                                 start = pos - 1;
344
345                                                 ttype = Token.Type.Token;
346                                                 while (pos < s.Length) {
347                                                         ch = s[pos];
348                                                         if (ch >= last_token_char || !token_chars[ch]) {
349                                                                 break;
350                                                         }
351
352                                                         ++pos;
353                                                 }
354                                         }
355
356                                         break;
357                                 }
358                         }
359
360                         return new Token (ttype, start, pos);
361                 }
362         }
363 }