Merge pull request #828 from mateusz-holenko/master
[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 bool TryCheck (string s)
63                         {
64                                 if (s == null)
65                                         return false;
66
67                                 return Lexer.IsValidToken (s);
68                         }
69
70                         public static void CheckQuotedString (string s)
71                         {
72                                 if (s == null)
73                                         throw new ArgumentNullException ();
74
75                                 var lexer = new Lexer (s);
76                                 if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
77                                         return;
78
79                                 if (s.Length == 0)
80                                         throw new ArgumentException ();
81
82                                 throw new FormatException (s);
83                         }
84
85                         public static void CheckComment (string s)
86                         {
87                                 if (s == null)
88                                         throw new ArgumentNullException ();
89
90                                 var lexer = new Lexer (s);
91
92                                 string temp;
93                                 if (!lexer.ScanCommentOptional (out temp)) {
94                                         if (s.Length == 0)
95                                                 throw new ArgumentException ();
96
97                                         throw new FormatException (s);
98                                 }
99                         }
100                 }
101
102                 public static class DateTime
103                 {
104                         public new static readonly Func<object, string> ToString = l => ((DateTimeOffset) l).ToString ("r", CultureInfo.InvariantCulture);
105                         
106                         public static bool TryParse (string input, out DateTimeOffset result)
107                         {
108                                 return Lexer.TryGetDateValue (input, out result);
109                         }
110                 }
111
112                 public static class EmailAddress
113                 {
114                         public static bool TryParse (string input, out string result)
115                         {
116                                 try {
117                                         new MailAddress (input);
118                                         result = input;
119                                         return true;
120                                 } catch {
121                                         result = null;
122                                         return false;
123                                 }
124                         }
125                 }
126
127                 public static class Int
128                 {
129                         public static bool TryParse (string input, out int result)
130                         {
131                                 return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
132                         }
133                 }
134
135                 public static class Long
136                 {
137                         public static bool TryParse (string input, out long result)
138                         {
139                                 return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
140                         }
141                 }
142
143                 public static class MD5
144                 {
145                         public static bool TryParse (string input, out byte[] result)
146                         {
147                                 try {
148                                         result = Convert.FromBase64String (input);
149                                         return true;
150                                 } catch {
151                                         result = null;
152                                         return false;
153                                 }
154                         }
155                 }
156
157                 public static class TimeSpanSeconds
158                 {
159                         public static bool TryParse (string input, out TimeSpan result)
160                         {
161                                 int value;
162                                 if (Int.TryParse (input, out value)) {
163                                         result = TimeSpan.FromSeconds (value);
164                                         return true;
165                                 }
166
167                                 result = TimeSpan.Zero;
168                                 return false;
169                         }
170                 }
171
172                 public static class Uri
173                 {
174                         public static bool TryParse (string input, out System.Uri result)
175                         {
176                                 return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
177                         }
178
179                         public static void Check (string s)
180                         {
181                                 if (s == null)
182                                         throw new ArgumentNullException ();
183
184                                 System.Uri uri;
185                                 if (!TryParse (s, out uri)) {
186                                         if (s.Length == 0)
187                                                 throw new ArgumentException ();
188
189                                         throw new FormatException (s);
190                                 }
191                         }
192                 }
193         }
194 }