Merge pull request #1857 from slluis/fix-assembly-resolver
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / Parser.cs
1 //
2 // Parser.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.Net.Mail;
30 using System.Globalization;
31 using System.Collections.Generic;
32
33 namespace System.Net.Http.Headers
34 {
35         static class Parser
36         {
37                 public static class Token
38                 {
39                         public static bool TryParse (string input, out string result)
40                         {
41                                 if (input != null && Lexer.IsValidToken (input)) {
42                                         result = input;
43                                         return true;
44                                 }
45
46                                 result = null;
47                                 return false;
48                         }
49
50                         public static void Check (string s)
51                         {
52                                 if (s == null)
53                                         throw new ArgumentNullException ();
54
55                                 if (!Lexer.IsValidToken (s)) {
56                                         if (s.Length == 0)
57                                                 throw new ArgumentException ();
58
59                                         throw new FormatException (s);
60                                 }
61                         }
62
63                         public static bool TryCheck (string s)
64                         {
65                                 if (s == null)
66                                         return false;
67
68                                 return Lexer.IsValidToken (s);
69                         }
70
71                         public static void CheckQuotedString (string s)
72                         {
73                                 if (s == null)
74                                         throw new ArgumentNullException ();
75
76                                 var lexer = new Lexer (s);
77                                 if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
78                                         return;
79
80                                 if (s.Length == 0)
81                                         throw new ArgumentException ();
82
83                                 throw new FormatException (s);
84                         }
85
86                         public static void CheckComment (string s)
87                         {
88                                 if (s == null)
89                                         throw new ArgumentNullException ();
90
91                                 var lexer = new Lexer (s);
92
93                                 string temp;
94                                 if (!lexer.ScanCommentOptional (out temp)) {
95                                         if (s.Length == 0)
96                                                 throw new ArgumentException ();
97
98                                         throw new FormatException (s);
99                                 }
100                         }
101                 }
102
103                 public static class DateTime
104                 {
105                         public new static readonly Func<object, string> ToString = l => ((DateTimeOffset) l).ToString ("r", CultureInfo.InvariantCulture);
106                         
107                         public static bool TryParse (string input, out DateTimeOffset result)
108                         {
109                                 return Lexer.TryGetDateValue (input, out result);
110                         }
111                 }
112
113                 public static class EmailAddress
114                 {
115                         public static bool TryParse (string input, out string result)
116                         {
117                                 try {
118                                         new MailAddress (input);
119                                         result = input;
120                                         return true;
121                                 } catch {
122                                         result = null;
123                                         return false;
124                                 }
125                         }
126                 }
127
128                 public static class Host
129                 {
130                         public static bool TryParse (string input, out string result)
131                         {
132                                 result = input;
133
134                                 System.Uri dummy;
135                                 return System.Uri.TryCreate ("http://u@" + input + "/", UriKind.Absolute, out dummy);
136                         }
137                 }
138
139                 public static class Int
140                 {
141                         public static bool TryParse (string input, out int result)
142                         {
143                                 return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
144                         }
145                 }
146
147                 public static class Long
148                 {
149                         public static bool TryParse (string input, out long result)
150                         {
151                                 return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
152                         }
153                 }
154
155                 public static class MD5
156                 {
157                         public new static readonly Func<object, string> ToString = l => Convert.ToBase64String ((byte[]) l);
158
159                         public static bool TryParse (string input, out byte[] result)
160                         {
161                                 try {
162                                         result = Convert.FromBase64String (input);
163                                         return true;
164                                 } catch {
165                                         result = null;
166                                         return false;
167                                 }
168                         }
169                 }
170
171                 public static class TimeSpanSeconds
172                 {
173                         public static bool TryParse (string input, out TimeSpan result)
174                         {
175                                 int value;
176                                 if (Int.TryParse (input, out value)) {
177                                         result = TimeSpan.FromSeconds (value);
178                                         return true;
179                                 }
180
181                                 result = TimeSpan.Zero;
182                                 return false;
183                         }
184                 }
185
186                 public static class Uri
187                 {
188                         public static bool TryParse (string input, out System.Uri result)
189                         {
190                                 return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
191                         }
192
193                         public static void Check (string s)
194                         {
195                                 if (s == null)
196                                         throw new ArgumentNullException ();
197
198                                 System.Uri uri;
199                                 if (!TryParse (s, out uri)) {
200                                         if (s.Length == 0)
201                                                 throw new ArgumentException ();
202
203                                         throw new FormatException (s);
204                                 }
205                         }
206                 }
207         }
208 }