Implemented more System.Net.Http.Headers
[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                 readonly Type type;
50
51                 public Token (Type type, int startPosition, int endPosition)
52                         : this ()
53                 {
54                         this.type = type;
55                         StartPosition = startPosition;
56                         EndPosition = endPosition;
57                 }
58
59                 public int StartPosition { get; private set; }
60                 public int EndPosition { get; private set; }
61
62                 public Type Kind {
63                         get {
64                                 return type;
65                         }
66                 }
67
68                 public static implicit operator Token.Type (Token token)
69                 {
70                         return token.type;
71                 }
72
73                 public override string ToString ()
74                 {
75                         return type.ToString ();
76                 }
77         }
78
79         class Lexer
80         {
81                 static readonly bool[] token_chars = {
82                                 false, false, false, false, false, false, false, false, false, false, false, false, false, false,
83                                 false, false, false, false, false, false, false, false, false, false, false, false, false, false,
84                                 false, false, false, false, false, true, false, true, true, true, true, false, false, false, true,
85                                 true, false, true, true, false, true, true, true, true, true, true, true, true, true, true, false,
86                                 false, false, false, false, false, false, true, true, true, true, true, true, true, true, true,
87                                 true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
88                                 false, false, false, true, true, true, true, true, true, true, true, true, true, true, true, true,
89                                 true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
90                                 false, true, false
91                         };
92
93                 static readonly int last_token_char = token_chars.Length;
94
95                 readonly string s;
96                 int pos;
97
98                 public Lexer (string stream)
99                 {
100                         this.s = stream;
101                 }
102
103                 public string GetStringValue (Token token)
104                 {
105                         return s.Substring (token.StartPosition, token.EndPosition - token.StartPosition);
106                 }
107
108                 public string GetStringValue (Token start, Token end)
109                 {
110                         return s.Substring (start.StartPosition, end.EndPosition - start.StartPosition);
111                 }
112
113                 public string GetQuotedStringValue (Token start)
114                 {
115                         return s.Substring (start.StartPosition + 1, start.EndPosition - start.StartPosition - 2);
116                 }
117
118                 public string GetRemainingStringValue (int position)
119                 {
120                         return position > s.Length ? null : s.Substring (position);
121                 }
122
123                 public bool TryGetNumericValue (Token token, out int value)
124                 {
125                         return int.TryParse (GetStringValue (token), out value);
126                 }
127
128                 public TimeSpan? TryGetTimeSpanValue (Token token)
129                 {
130                         int seconds;
131                         if (TryGetNumericValue (token, out seconds)) {
132                                 return TimeSpan.FromSeconds (seconds);
133                         }
134
135                         return null;
136                 }
137
138                 public bool TryGetDateValue (Token token, out DateTimeOffset value)
139                 {
140                         string text = token == Token.Type.QuotedString ?
141                                 s.Substring (token.StartPosition + 1, token.EndPosition - token.StartPosition - 2) :
142                                 GetStringValue (token);
143
144                         const DateTimeStyles DefaultStyles = DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces;
145
146                     return
147                                 DateTimeOffset.TryParseExact (text, "r", DateTimeFormatInfo.InvariantInfo, DefaultStyles, out value) ||
148                                 DateTimeOffset.TryParseExact (text, "dddd, dd'-'MMM'-'yy HH:mm:ss 'GMT'", DateTimeFormatInfo.InvariantInfo, DefaultStyles, out value) ||
149                                 DateTimeOffset.TryParseExact (text, "ddd MMM d HH:mm:ss yyyy", DateTimeFormatInfo.InvariantInfo, DefaultStyles, out value);
150                 }
151
152                 public static bool IsValidToken (string input)
153                 {
154                         int i = 0;
155                         //
156                         // any CHAR except CTLs or separator
157                         //
158                         for (; i < input.Length; ++i) {
159                                 char s = input[i];
160                                 if (s > last_token_char || !token_chars[s])
161                                         return false;
162                         }
163
164                         return i > 0;
165                 }
166
167                 public void EatChar ()
168                 {
169                         ++pos;
170                 }
171
172                 public int PeekChar ()
173                 {
174                         return pos < s.Length ? s[pos] : -1;
175                 }
176
177                 public bool ScanCommentOptional (out string value)
178                 {
179                         var t = Scan ();
180                         if (t != Token.Type.OpenParens) {
181                                 value = null;
182                                 return t == Token.Type.End;
183                         }
184
185                         while (pos < s.Length) {
186                                 var ch = s[pos];
187                                 if (ch == ')') {
188                                         ++pos;
189                                         var start = t.StartPosition;
190                                         value = s.Substring (start, pos - start);
191                                         return true;
192                                 }
193
194                                 // any OCTET except CTLs, but including LWS
195                                 if (ch < 32 || ch > 126)
196                                         break;
197
198                                 ++pos;
199                         }
200
201                         value = null;
202                         return false;
203                 }
204
205                 public Token Scan ()
206                 {
207                         int start = pos;
208                         if (s == null)
209                                 return new Token (Token.Type.Error, 0, 0);
210
211                         Token.Type ttype;
212                         if (pos >= s.Length) {
213                                 ttype = Token.Type.End;
214                         } else {
215                                 ttype = Token.Type.Error;
216                         start:
217                                 char ch = s[pos++];
218                                 switch (ch) {
219                                 case ' ':
220                                 case '\t':
221                                         if (pos == s.Length) {
222                                                 ttype = Token.Type.End;
223                                                 break;
224                                         }
225
226                                         goto start;
227                                 case '=':
228                                         ttype = Token.Type.SeparatorEqual;
229                                         break;
230                                 case ';':
231                                         ttype = Token.Type.SeparatorSemicolon;
232                                         break;
233                                 case '/':
234                                         ttype = Token.Type.SeparatorSlash;
235                                         break;
236                                 case '-':
237                                         ttype = Token.Type.SeparatorDash;
238                                         break;
239                                 case ',':
240                                         ttype = Token.Type.SeparatorComma;
241                                         break;
242                                 case '"':
243                                         // Quoted string
244                                         start = pos - 1;
245                                         while (pos < s.Length) {
246                                                 ch = s[pos];
247                                                 if (ch == '"') {
248                                                         ++pos;
249                                                         ttype = Token.Type.QuotedString;
250                                                         break;
251                                                 }
252
253                                                 // any OCTET except CTLs, but including LWS
254                                                 if (ch < 32 || ch > 126)
255                                                         break;
256
257                                                 ++pos;
258                                         }
259
260                                         break;
261                                 case '(':
262                                         start = pos - 1;
263                                         ttype = Token.Type.OpenParens;
264                                         break;
265                                 default:
266                                         if (ch <= last_token_char && token_chars[ch]) {
267                                                 start = pos - 1;
268
269                                                 ttype = Token.Type.Token;
270                                                 while (pos < s.Length) {
271                                                         ch = s[pos];
272                                                         if (ch > last_token_char || !token_chars[ch]) {
273                                                                 break;
274                                                         }
275
276                                                         ++pos;
277                                                 }
278                                         }
279
280                                         break;
281                                 }
282                         }
283
284                         return new Token (ttype, start, pos);
285                 }
286         }
287 }