BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[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
32 namespace System.Net.Http.Headers
33 {
34         static class Parser
35         {
36                 public static class Token
37                 {
38                         public static bool TryParse (string input, out string result)
39                         {
40                                 if (input != null && Lexer.IsValidToken (input)) {
41                                         result = input;
42                                         return true;
43                                 }
44
45                                 result = null;
46                                 return false;
47                         }
48
49                         public static void Check (string s)
50                         {
51                                 if (s == null)
52                                         throw new ArgumentNullException ();
53
54                                 if (!Lexer.IsValidToken (s)) {
55                                         if (s.Length == 0)
56                                                 throw new ArgumentException ();
57
58                                         throw new FormatException (s);
59                                 }
60                         }
61
62                         public static void CheckQuotedString (string s)
63                         {
64                                 if (s == null)
65                                         throw new ArgumentNullException ();
66
67                                 var lexer = new Lexer (s);
68                                 if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
69                                         return;
70
71                                 if (s.Length == 0)
72                                         throw new ArgumentException ();
73
74                                 throw new FormatException (s);
75                         }
76
77                         public static void CheckComment (string s)
78                         {
79                                 if (s == null)
80                                         throw new ArgumentNullException ();
81
82                                 var lexer = new Lexer (s);
83
84                                 string temp;
85                                 if (!lexer.ScanCommentOptional (out temp)) {
86                                         if (s.Length == 0)
87                                                 throw new ArgumentException ();
88
89                                         throw new FormatException (s);
90                                 }
91                         }
92                 }
93
94                 public static class DateTime
95                 {
96                         public new static readonly Func<object, string> ToString = l => ((DateTimeOffset) l).ToString ("r", CultureInfo.InvariantCulture);
97                         
98                         public static bool TryParse (string input, out DateTimeOffset result)
99                         {
100                                 return Lexer.TryGetDateValue (input, out result);
101                         }
102                 }
103
104                 public static class EmailAddress
105                 {
106                         public static bool TryParse (string input, out string result)
107                         {
108                                 try {
109                                         new MailAddress (input);
110                                         result = input;
111                                         return true;
112                                 } catch {
113                                         result = null;
114                                         return false;
115                                 }
116                         }
117                 }
118
119                 public static class Int
120                 {
121                         public static bool TryParse (string input, out int result)
122                         {
123                                 return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
124                         }
125                 }
126
127                 public static class Long
128                 {
129                         public static bool TryParse (string input, out long result)
130                         {
131                                 return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
132                         }
133                 }
134
135                 public static class MD5
136                 {
137                         public static bool TryParse (string input, out byte[] result)
138                         {
139                                 try {
140                                         result = Convert.FromBase64String (input);
141                                         return true;
142                                 } catch {
143                                         result = null;
144                                         return false;
145                                 }
146                         }
147                 }
148
149                 public static class TimeSpanSeconds
150                 {
151                         public static bool TryParse (string input, out TimeSpan result)
152                         {
153                                 int value;
154                                 if (Int.TryParse (input, out value)) {
155                                         result = TimeSpan.FromSeconds (value);
156                                         return true;
157                                 }
158
159                                 result = TimeSpan.Zero;
160                                 return false;
161                         }
162                 }
163
164                 public static class Uri
165                 {
166                         public static bool TryParse (string input, out System.Uri result)
167                         {
168                                 return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
169                         }
170
171                         public static void Check (string s)
172                         {
173                                 if (s == null)
174                                         throw new ArgumentNullException ();
175
176                                 System.Uri uri;
177                                 if (!TryParse (s, out uri)) {
178                                         if (s.Length == 0)
179                                                 throw new ArgumentException ();
180
181                                         throw new FormatException (s);
182                                 }
183                         }
184                 }
185         }
186 }