Merge pull request #1542 from ninjarobot/UriTemplateMatchException
[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 TimeSpan? TryGetTimeSpanValue (Token token)
157                 {
158                         int seconds;
159                         if (TryGetNumericValue (token, out seconds)) {
160                                 return TimeSpan.FromSeconds (seconds);
161                         }
162
163                         return null;
164                 }
165
166                 public bool TryGetDateValue (Token token, out DateTimeOffset value)
167                 {
168                         string text = token == Token.Type.QuotedString ?
169                                 s.Substring (token.StartPosition + 1, token.EndPosition - token.StartPosition - 2) :
170                                 GetStringValue (token);
171
172                         return TryGetDateValue (text, out value);
173                 }
174
175                 public static bool TryGetDateValue (string text, out DateTimeOffset value)
176                 {
177                         const DateTimeStyles DefaultStyles = DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces;
178
179                         return DateTimeOffset.TryParseExact (text, dt_formats, DateTimeFormatInfo.InvariantInfo, DefaultStyles, out value);
180                 }
181
182                 public bool TryGetDoubleValue (Token token, out double value)
183                 {
184                         string s = GetStringValue (token);
185                         return double.TryParse (s, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out value);
186                 }
187
188                 public static bool IsValidToken (string input)
189                 {
190                         int i = 0;
191                         //
192                         // any CHAR except CTLs or separator
193                         //
194                         for (; i < input.Length; ++i) {
195                                 char s = input[i];
196                                 if (!IsValidCharacter (s))
197                                         return false;
198                         }
199
200                         return i > 0;
201                 }
202
203                 public static bool IsValidCharacter (char input)
204                 {
205                         return input < last_token_char && token_chars[input];
206                 }
207
208                 public void EatChar ()
209                 {
210                         ++pos;
211                 }
212
213                 public int PeekChar ()
214                 {
215                         return pos < s.Length ? s[pos] : -1;
216                 }
217
218                 public bool ScanCommentOptional (out string value)
219                 {
220                         Token t;
221                         if (ScanCommentOptional (out value, out t))
222                                 return true;
223
224                         return t == Token.Type.End;
225                 }
226
227                 public bool ScanCommentOptional (out string value, out Token readToken)
228                 {
229                         readToken = Scan ();
230                         if (readToken != Token.Type.OpenParens) {
231                                 value = null;
232                                 return false;
233                         }
234
235                         int parens = 1;
236                         while (pos < s.Length) {
237                                 var ch = s[pos];
238                                 if (ch == '(') {
239                                         ++parens;
240                                         ++pos;
241                                         continue;
242                                 }
243
244                                 if (ch == ')') {
245                                         ++pos;
246                                         if (--parens > 0)
247                                                 continue;
248
249                                         var start = readToken.StartPosition;
250                                         value = s.Substring (start, pos - start);
251                                         return true;
252                                 }
253
254                                 // any OCTET except CTLs, but including LWS
255                                 if (ch < 32 || ch > 126)
256                                         break;
257
258                                 ++pos;
259                         }
260
261                         value = null;
262                         return false;
263                 }
264
265                 public Token Scan (bool recognizeDash = false)
266                 {
267                         int start = pos;
268                         if (s == null)
269                                 return new Token (Token.Type.Error, 0, 0);
270
271                         Token.Type ttype;
272                         if (pos >= s.Length) {
273                                 ttype = Token.Type.End;
274                         } else {
275                                 ttype = Token.Type.Error;
276                         start:
277                                 char ch = s[pos++];
278                                 switch (ch) {
279                                 case ' ':
280                                 case '\t':
281                                         if (pos == s.Length) {
282                                                 ttype = Token.Type.End;
283                                                 break;
284                                         }
285
286                                         goto start;
287                                 case '=':
288                                         ttype = Token.Type.SeparatorEqual;
289                                         break;
290                                 case ';':
291                                         ttype = Token.Type.SeparatorSemicolon;
292                                         break;
293                                 case '/':
294                                         ttype = Token.Type.SeparatorSlash;
295                                         break;
296                                 case '-':
297                                         if (recognizeDash) {
298                                                 ttype = Token.Type.SeparatorDash;
299                                                 break;
300                                         }
301
302                                         goto default;
303                                 case ',':
304                                         ttype = Token.Type.SeparatorComma;
305                                         break;
306                                 case '"':
307                                         // Quoted string
308                                         start = pos - 1;
309                                         while (pos < s.Length) {
310                                                 ch = s [pos++];
311                                                 if (ch == '"') {
312                                                         ttype = Token.Type.QuotedString;
313                                                         break;
314                                                 }
315                                         }
316
317                                         break;
318                                 case '(':
319                                         start = pos - 1;
320                                         ttype = Token.Type.OpenParens;
321                                         break;
322                                 default:
323                                         if (ch < last_token_char && token_chars[ch]) {
324                                                 start = pos - 1;
325
326                                                 ttype = Token.Type.Token;
327                                                 while (pos < s.Length) {
328                                                         ch = s[pos];
329                                                         if (ch >= last_token_char || !token_chars[ch]) {
330                                                                 break;
331                                                         }
332
333                                                         ++pos;
334                                                 }
335                                         }
336
337                                         break;
338                                 }
339                         }
340
341                         return new Token (ttype, start, pos);
342                 }
343         }
344 }