Merge pull request #819 from brendanzagaeski/patch-1
[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 Int
129                 {
130                         public static bool TryParse (string input, out int result)
131                         {
132                                 return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
133                         }
134                 }
135
136                 public static class Long
137                 {
138                         public static bool TryParse (string input, out long result)
139                         {
140                                 return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
141                         }
142                 }
143
144                 public static class MD5
145                 {
146                         public static bool TryParse (string input, out byte[] result)
147                         {
148                                 try {
149                                         result = Convert.FromBase64String (input);
150                                         return true;
151                                 } catch {
152                                         result = null;
153                                         return false;
154                                 }
155                         }
156                 }
157
158                 public static class TimeSpanSeconds
159                 {
160                         public static bool TryParse (string input, out TimeSpan result)
161                         {
162                                 int value;
163                                 if (Int.TryParse (input, out value)) {
164                                         result = TimeSpan.FromSeconds (value);
165                                         return true;
166                                 }
167
168                                 result = TimeSpan.Zero;
169                                 return false;
170                         }
171                 }
172
173                 public static class Uri
174                 {
175                         public static bool TryParse (string input, out System.Uri result)
176                         {
177                                 return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
178                         }
179
180                         public static void Check (string s)
181                         {
182                                 if (s == null)
183                                         throw new ArgumentNullException ();
184
185                                 System.Uri uri;
186                                 if (!TryParse (s, out uri)) {
187                                         if (s.Length == 0)
188                                                 throw new ArgumentException ();
189
190                                         throw new FormatException (s);
191                                 }
192                         }
193                 }
194         }
195 }